Laravel #foreach - php

I need to get each comment of news . Its working good for firstorFail() item
{{$news->comments()->firstOrFail()->name}}
But bring me empty result when im try this one with foreach:
#foreach($news->comments() as $comment)
{{$comment->name}}
#endforeach

The function firstOrFail will return exactly one comment. Seems like you actually want all comments? You should use this in your controller
$news->comments; // yeah that's it, it will load all comments
Also return news
return view('my.view', compact('news'));
Then use it in blade
#foreach($news->comments as $comment)
{{$comment->name}}
#endforeach
the line
$news->comments()
would require you to also call ->get() , because comments() will return the relation instead of the actual data.

This: $news->comments() bringing you query builder probably. So what you need is to ->get() for execute query like this:
#foreach($news->comments()->get() as $comment)
{{$comment->name}}
#endforeach
Edit
or as a #Roy Philips mentioned: $news->comments

Related

Laravel - getting relation in loop - best practices

Just an example:
let's say I have Post model, and the Comment model. Post, of course, have Comments, one-to-many relation.
I have to display list of posts with comments below it.
I'll get my posts in the controller:
$posts = Post::get(), I'll pass it to the blade view and then I'll loop through it
#foreach($posts as $post)
{{ $post->title }}
{{ $post->comments }}
#endforeach
where $post->comments is some relation
public function comments()
{
return $this->hasMany(Comment::class);
}
As we know, that query will be executed many times.
Now my question: how we should optimize it?
Return Cache::remember in the getter?
Get (somehow?) those comments, when getting the posts in one query? Something like join query? I know that I can write that kind of query, but I'm talking about Eloquent's query builder. And then how get the comments within the loop? Wouldn't {{ $post->comments }} call the relation again instead of getting stored data?
Different solution?
You can do $posts = Post::with('comments')->get() to eager load the comments with the post. Read more about it in the documentation: https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
Also, to display the comments you would want to add another foreach loop. It would look something like this:
#foreach($posts as $post)
{{ $post->title }}
#foreach($post->comments as $comment)
{{ $comment->title }}
#endforeach
#endforeach
You’ve probably cached some model data in the controller before, but I am going to show you a Laravel model caching technique that’s a little more granular using Active Record models
Note that we could also use the Cache::rememberForever() method and rely on our caching mechanism’s garbage collection to remove stale keys. I’ve set a timer so that the cache will be hit most of the time, with a fresh cache every fifteen minutes.
The cacheKey() method needs to make the model unique, and invalidate the cache when the model is updated. Here’s my cacheKey implementation:
public function cacheKey()
{
return sprintf(
"%s/%s-%s",
$this->getTable(),
$this->getKey(),
$this->updated_at->timestamp
);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function getCachedCommentsCountAttribute()
{
return Cache::remember($this->cacheKey() . ':comments_count', 15, function () {
return $this->comments->count();
});
}
yes u can do like that in controller
$minutes = 60;
$posts = Cache::remember('posts', $minutes, function () {
return Post::with('comments')->get()
});
in blade u can get like that
#foreach($posts as $post)
{{ $post->title }}
#foreach($post->comments as $comment)
{{ $comment->title }}
#endforeach
#endforeach
for more information read this article

Paginate Issue Laravel 5.6

Was wondering if someone could check this out and let me know what I'm doing wrong. I'm trying to use paginate on a database query and render it in my view.
Is my DB query incorrect? I was following the docs to create it. Also read that I needed to remove ->get() when using paginate.
This is what is giving the error on my view: {{$item->paginate(4)}}, same happens if I use {{$item->links(4)}}
Everything renders fine if I remove paginate from the query in the controller..?
Here is what I'm working with.
Controller:
public function index()
{
// $news = DB::table('news')->orderBy('id', 'DESC')->paginate(4);
$news = DB::table('news')->select('id','title','description','listing_image','created_at','updated_at')->orderBy('id', 'DESC')->paginate(4);
return view('news.index',compact('news'));
}
View: (Getting error: Call to undefined method stdClass::paginate())
#if ($news->count())
#foreach($news as $item)
...html
#if ($item->listing_image)
...more html
#else
#endif
... more html
#endforeach
{{$item->paginate(4)}}
#endif
change {{$item->paginate(4)}} to {{ $news->links() }}

Laravel: how to access record by id in view

In my controller I return a view with a ?collection $programs? from an eloquent query to the view.
Controller
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
return View::make('admin/register_users/show', compact(programs));
I wan to do something like this without it running a new query from the view...
VIEW
{{$program->find(id)}}
I know that $programs is a dataset that already has the record, but I don't know the way to access the element by ID this way.
How do I do this?
(sorry, seems like an obviously searchable question but my search terms aren't comming up with the answer)
in that case you need to make a #foreach in $programs to access the data. Like this:
#foreach($programs as $key => $value)
{{$value->id}}
#endforeach
If the return is only one line you can do this:
{{$programs[0]->id}}
1.You have to add the method get (converts the "dataset " in a Laravel collection) to iterate the collection.
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
->get()
2.If you want to get a single record:
ScheduledProgram::where('registration_start_date', '<=', $today)->where('id', 5)->get();

How to manipulate an object in laravel

I have a laravel query as the one below:
$campaign = Campaign::with(array('tracks.flights' => function($q) use ($dates)
{
$q->whereRaw("flights.start_date BETWEEN '". $dates['start']."' AND '".$dates['end']."'")->orderBy('start_date')
->with('asset')
->with('comments');
}
))
->with('tracks.group')
->with('tracks.media')
->orderBy('created_at', 'desc')
->find($id);
I am very new to laravel and right now the response from this query returns all the data required including the comments with the comments attributes from the DB.
What i want to achieve is manipulate the comments object so it includes the user name from the users table as the comments table has the user_id only as an attribute.
How can I achieve this? I am very new to laravel.
Your Comment model must have a relationship with the User model, such as:
public function user()
{
return $this->belongsTo('App\User');
}
Now when you are iterating comments you are able to do $comment->user->name or in your case it will be something like this:
#if(!$campaign->tracks->flights->isEmpty())
#foreach($campaign->tracks->flights as $flight)
#if(!$flight->comments->isEmpty())
#foreach($flight->comments as $comment)
{!! $comment->user->name !!}
#endforeach
#endif
#endforeach
#endif
Once you can do this, next step is to understand eager load with eloquent.

Laravel 4 Eager Loading "Undefined Property"

Why can't I access the property in an eager loaded senario?
I am trying to access the photo location for my facilities model. They have a Many:Many relationship. When I load the facility info using
$facilities = Facility::with('photos')->get();
When I try to access
foreach($facilities as $facility)
{
echo $facility->photos->id;
}
I get the Undefined property: Illuminate\Database\Eloquent\Collection::$id
If I echo $facilities->photos I end up with.
[{"id":"3",
"name":null,
"location":"facilities\/facility1.jpg\r",
"created_at":"2013-07-18 14:15:19",
"deleted_at":null,
"updated_at":null,
"pivot":{"facility_id":"5","photo_id":"3"}}]
Whats the best way to access any property in this array?
With eager loading the photos you get a photos collection and in order to get the photo you need to iterate through the photos collection e.g.
foreach($facilities as $facility)
{
foreach($facility->photos as $photo)
{
echo $photo->id;
}
}
If you only want just the first photo you can get it by calling the first() method on the collection
foreach($facilities as $facility)
{
$facility->photos->first()->id;
}
What is received from the database is an array with one object. This is why a foreach works. To access it the way you want you should add the first() method just as Atrim says, you can even drop the get() method. Only I would suggest doing it in the controller.
$facilities = Facility::with('photos')->first();
{{ $facilities->location }}
Thanks Altim, I just hit this problem too and the nested foreach is one of the solutions.
Nevertheless, I'll share with you guys the solution that I used at the end.
Instead of the nested foreach or even the eager loader, I used a Join Query Builder. In my case:
$posts = DB::table('posts')
->leftJoin('users', 'posts.user_id', '=', 'users.id')
->get();
This allowed me to use one foreach, and all the data is at the same level, in my case for instance would be something like:
#foreach ($posts as $post)
<p>{{ $post->first_name }}</p> # From users table
<p>{{ $post->title }}</p> # From posts table
<p>{{ $post->body }}</p>
#endforeach
Maybe it is not answering this question exactly , but hope it helps to others like me that ended up in this particular question.

Categories