I want to sort/order my data by its release_date but I get an error I don't understand:
In my Controller I have:
public function index() {
$releases = Release::orderBy('release_date', 'desc');
return view('pages.index')->with('releases', $releases);
}
and in my view file I have this:
#if (count($releases) > 0)
#foreach ($releases as $release)
<div>
<p>{{$release->artist}} - {{$release->album_title}} <span>ReleaseDate: {{$release->release_date}}</span></p>
</div>
#endforeach
#endif
This returns the error:
count(): Parameter must be an array or an object that implements
Countable
What does this mean? Can someone help me out?
Try this code:
public function index()
{
$releases = Release::orderBy('release_date', 'desc')->get();
return view('pages.index')->with('releases', $releases);
}
The reason you code wasn't working before is because orderBy won't perform the query, it will simply add to it. In this case you need to chain on get() at the end in order to perform the query and get the results otherwise you will just get an instance of the query builder.
Change Controller
public function index() {
$releases = Release::orderBy('release_date', 'desc')->get();
return view('pages.index',compact('releases');
}
change View
#if ($releases->count() > 0)
#foreach ($releases as $release)
<div>
<p>{{$release->artist}} - {{$release->album_title}} <span>ReleaseDate: {{$release->release_date}}</span></p>
</div>
#endforeach
#endif
Related
I'm a beginner in Laravel
public function allorders($param1){
$customerss=Customer::where('mobile_number',$param1)->first();
$customer_id1=$customerss->id;
$orderss= Order::where('customer_id',$customer_id1);
return view('admin.allorders')->with('orderss', $orderss);
}
and i have the view admin.allorders
#foreach ($orderss as $tag)
<span class="label label-primary">{{ $tag['customer_id']}}</span>
#endforeach
I'm sure the $orderss is having data but it's not shown in the view.
You need to add get() to execute the query:
$orderss = Order::where('customer_id', $customer_id1)->get();
Also, you could use relationships instead of this:
$customerss=Customer::where('mobile_number',$param1)->first();
$customer_id1=$customerss->id;
$orderss= Order::where('customer_id',$customer_id1);
You could do the same with just one query:
$orderss = Order::whereHas('customer', function($q) use($param1) {
$q->where('mobile_number', $param1);
})->get();
To make it work, define this relationship in the Order model:
public function customer()
{
return $this->belongsTo(Customer::class);
}
may i know why this code showing invalid argument supplied for foreach() and how i should correct it ?
#foreach($project_details as $project) {
#foreach($project->projectusers as $users) {
{{ $user->name }}
}
#endforeach }
#endforeach
Controller
public function index()
{
$this->project = Project::with('projectusers')->get();
return view('projects.index', [
'project_details' => $this->project
]);
}
projectusers is a relationship ? Do you use the with() function in your Eloquent query to get that relationship within the collection ?
In addition, in your foreach code, you use the plural for users ($project->projectusers as $users), and then in the double brackets, you use the singular {{ $user->name }}
Remove the 's' from users in the foreach and it should work.
I would suggest reading up on foreach() a little more:
http://php.net/manual/en/control-structures.foreach.php
But if this is your entire code, you have not set any array values or assigned any values from a database or anything
$arr = array(1, 2, 3, 4);
Using the website above you can see it is quite simple to fill an array yourself depending on how many values you need to be added.
Just use compact as a second argument of your view()
public function index()
{
$projects = Project::with('projectusers')->get();
return view('projects.index', compact('projects'));
}
Then in your projects.index just call :
#foreach($projects as $project) {
#foreach($project->projectusers as $users) {
{{ $user->name }}
}
#endforeach }
#endforeach
Try this one:
public function index() {
$projects = Project::whereHas('user')->get();
return view('projects.index', compact('projects''));
in your blade:
#foreach($projects as $project)
{{$project->user->name}}
#endforeach
I have undefined variable and it doesn't call variable.
I get missing argument 1 error when I try accessing a page. This is my code.
Part of the view:
#foreach($stations as $station)
<span> {{ $stations->station }} </span>
#endforeach
Controller:
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration', $stations);
}
Route:
Route::get('configuration/', 'ConfigurationController#show');
Try with below code in controller
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
$data['stations'] = $stations;
return view('configuration.configuration', $data);
}
View
#foreach($stations as $station)
<span> {{ $station->station }} </span>
#endforeach
You are directly passing the array value to view. It will not work like that. You have to assign the values to an array index and then call that index like $index_name in view. Then it will give you the desired output
or
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration')->with(stations);
}
if you dont want to neft your $station into unused $data
Try with below code in controller:-
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration', compact('stations'));
}
I have the following relationship between my tables
In my Models i have the following code:
Location Model:
public function member() {
return $this->belongsTo('App\Member','member_id');
}
Member Model:
public function locations(){
return $this->hasMany('App\Location','member_id');
}
Now in my Location controller I created the following function.
public function getFinalData(){
$locations = Location::with('member')->whereNotNull('member_id')->get();
return view('locations.final-list',['locations'=>$locations]);
}
In my blade template however, I am unable to iterate through the member properties
<ul>
#foreach($locations as $location)
<li>{{$location->id}}</li>
<li>
#foreach($location->member as $member)
{{$member->id}}
#endforeach
</li>
#endforeach
</ul>
This gives me the following error:
Trying to get property of non-object (View:locations/final-list.blade.php)
update: The result i'm trying to achieve corresponds to this query
SELECT locations.location_id,locations.name,members.first_name, members.last_name , locations.meters
FROM locations,members
WHERE locations.member_id = members.id
Update 2:
So i tried to access a single location with a single attached to it and that works perfectly
public function getFinalData(){
//$locations = Location::with('member')->whereNotNull('member_id')->get();
$location = Location::find(0);
return view('locations.final-list',['location'=>$location]);
}
in final-list
$location->member->first_name
**Update 3 **
Tinker Output for one record :
In your Location model, rewrite below function:-
public function member()
{
return $this->belongsTo('App\Member', 'id');
}
Get all locations with member detail as below:-
$locations = Location::with('member')->get();
Hope it will work for you :-)
For showing related tables information in blade is as shown:
Model Relations
Location Model:
public function member() {
return $this->belongsTo('App\Member','member_id');
}
Member Model:
public function locations(){
return $this->hasMany('App\Location','member_id');
}
Get all locations in your controller
$locations = Location::all();
return view('locations.final-list', compact('locations'));
Or
$locations = Location::orderBy('id')->get();
return view('locations.final-list', compact('locations'));
Inside your view(blade) write the following code:
<div>
#if($locations)
#foreach($locations AS $location)
<div>{{ $location->id }}</div>
#if($location->member)
#foreach($location->member AS $member)
<div>
{{ $member->id }}
</div>
#endforeach
#endif
#endforeach
#endif
</div>
Okay so I just started learning Laravel and its fantastic. I just got stuck trying to retrive all the post from a user. Here is my code
models/User.php
public function posts()
{
$this->hasMany('Post');
}
models/Post.php
public function user()
{
$this->belongsTo('User');
}
controller/UserController.php
public function getUserProfile($username)
{
$user = User::where('username', '=', $username)->first();
$this->layout->content = View::make('user.index', array('user'=>$user));
}
views/user/index.blade.php
<div class="show-post">
<ul>
<li>{{ $user->posts }}</lo>
</ul>
</div>
I also tried:
#foreach($user->posts as $post)
<li>{{$post->post}}</li>
#endforeach
So im having trouble displaying the post for each specific user. Thank you.
So with the help of #WereWolf- The Alpha I was able to solve it and make my code better, If you notice I forgot to return my relationship functions. example:
Notice I hadn't returned it before
models/Post.php
public function users()
{
return $this->belongsTo('users');
}
But also the way I was querying my database was inefficient so Alpha showed me Eager Loading
http://laravel.com/docs/eloquent#eager-loading
example of controller
public function getUserProfile($username)
{
$user = User::with('posts')->where('username', '=', $username)->first();
$this->layout->content = View::make('user.index', array('user'=>$user));
}
and finally the view:
div class="show-post">
#foreach($user->posts as $post)
{{ $post->post }}
#endforeach
</div>
Actually $user->posts returns a collection of Post model so when you try this
{{ $user->posts }}
Laravel tries to echo that collection object which is not possible. You may try foreach loop instead:
#foreach($user->posts as $post)
{{ $post->somepropertyname }}
#endforeach
It's also possible to do something like this:
// Get first post->somepropertyname
{{ $user->posts->first()->somepropertyname }}
// Get last post->somepropertyname
{{ $user->posts->last()->somepropertyname }}
// Get first post->somepropertyname (same as first)
{{ $user->posts->get(0)->somepropertyname }}
// Get second post->somepropertyname from collection
{{ $user->posts->get(1)->somepropertyname }}
// Get the post->somepropertyname by id (post id) 2
{{ $user->posts->find(2)->somepropertyname }}
Also you may use eager loading like this (better):
$user = User::with('posts')->where('username', '=', $username)->first();
You may like this article about Cocllection.
Update: Also use return in model methods, like:
public function posts()
{
return $this->hasMany('Post');
}