I have post table and comments table. I want to get all posts with its comments and the following returns just post which has comments:
Post::with('comments')
UPDATE
for example
post_table
id post
1 post1
2 post2
comment table
id post_id comment
1 1 sapmle_comment
Post::with('comments') returns only that posts, which have comments, it returns only first post, becouse second post doesnot have comments, i want to get all post (with or without comments)
Your question is not clear enough but to get all the posts with comments you may try this:
$posts = Post::with('comments')->get();
To get posts only which has comments you may try this:
$posts = Post::has('comments')->get();
I'm not too sure what your question is, but I am assuming that it is not returning your comments. If your not able to return posts that do not have comments Sheikh's answer will work for you. If you are not able to retrieve the comments for each post you should make sure your model has defined:
public function comments(){
return $this->hasMany('Comment');
}
and then make sure your comments model has:
public function post(){
return $this->belongsTo('Post');
}
If you want to pass this along to your view from the controller you can:
$comments = $post->comments()->orderBy('created_at')->get();
return View::make('view', compact('comments'));
You can loop through each of the comments by:
#foreach ($comments as $comment)
{{$commment->content}}
#endforeach
Related
I have a table users and posts with columns user_id and post_views.
In post_views I keep information how many times post was display.
And now, in query I would like to get user with sum of post_views all his posts.
I tried do something like this:
User::where(['id'=>$id])->with('posts')->get();
And in model I defined:
public function posts()
{
return $this->hasMany('App\Models\Post')->sum('post_views','AS','totalViews');
}
But without success.
How to do it?
Thank you
You can use a modified withCount():
public function posts()
{
return $this->hasMany('App\Models\Post');
}
$user = User::withCount(['posts as post_views' => function($query) {
$query->select(DB::raw('sum(post_views)'));
}])->find($id);
// $user->post_views
You can use
User::withCount('posts')->find($id)
to get the user with the id $id and a posts_count attribute in the response
I'm not fully sure what the intention of ->sum('game_plays','AS','totalVies'); is - you would need to add more context if you want this
Just something to add with regards to your shown code: No need to query by id using where + the get() at the end will make you query for a collection. If you want to get a single result use find when searching by id
As always laravel has a method for that : withSum (Since Laravel v8)
Note : I know that at the time of the message was posted, the method did not exist, but since I came across this page when I was looking for the same result, I though it might be interesting to share.
https://laravel.com/docs/9.x/eloquent-relationships#other-aggregate-functions
In your case it should be :
$user = User::withSum('posts as total_views', 'post_views')->find($id);
Then you can access to the result :
$user->total_views
I have two comments associated with a post, but the code to count the comments associated with the posts shows "1". Do you know why?
code to count the comments:
$commentsCount= Post::with('comments')->count();
A post can have multiple comments, a comment is associated with a post.
Comment model:
public function post(){
return $this->belongsTo('App\Post');
}
Post model:
public function comments(){
return $this->hasMany('App\Comment', 'post_id');
}
Do you know why?
Calling with will eager load the comments onto the Post model but still only return one Post object.
You need to call comments on a post object to get a collection back that you can count.
Judging by the code you have if you do something along the lines of
$post = Post::find(1);
$commentsCount = $post->comments->count();
You should get what you are looking for.
Hello guys I am using Laravel 5.6 I have three tables in the database posts,comments and replies and three models Post , Comment , Reply
the relations are as follow one post has many comments and one comment has many replies. I created a route that when hit will return some data ;however, I want this data to be in a specific way read this example:
Lets say I have 6 posts in my database and each post has 6 comments also each comment has 6 replies I want to return only the first 3 posts along with the first 3 comments for each post also the first 3 replies for each comment
//this is a function inside a controller
//and for sure I have make sure to make use of the models namespaces
public function test(){
$posts = Post::with(['comments' => function($data){
return $data->take(3);
},
'comments.replies' => function($data){
return $data->take(3);
}])->paginate(3);
//returning the posts
return $posts
}
This way is working it returns the first 3 post and it returns the first 3 comments and first 3 replies only for the first post but for other posts I only get an empty key of comments so there is no replies as a result
hope you get my question please help
sorry for big question
Thanks in advance.
There is no native support for this in Laravel.
I created a package for it: https://github.com/staudenmeir/eloquent-eager-limit
Use the HasEagerLimit trait in both the parent and the related model.
class Post extends Model {
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
class Comment extends Model {
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
Then you can apply ->take(3) to your relationship.
The same goes for the replies.
This question already has answers here:
Laravel eloquent counting a relation
(3 answers)
Closed 5 years ago.
I am trying to make a query where I get the number of comments on a News in a row in laravel.
News Table
News_Comments Table
Display count of comments in 'Kommentare'
This is the query in mysql.
SELECT news.Id,news.title,news.body,news.created_at, COUNT(news_comments.id) AS Kommentare
FROM news
LEFT JOIN news_comments ON news_comments.fk_news_id = news.id
GROUP BY news.id
Result of this query
How would I write that in laravel 5.5?
Thank you very much!
You have to set the relationship between News and NewsComments.
In News Model, supposing that news_comments is in NewsComment Model, in App folder. Set the relationship:
public function newsComments()
{
return $this->hasMany('App\NewsComment', 'fk_news_id');
}
And then, to get the count, you can use count() method:
$count = $news->newsComments()->count();
Or, directly using blade:
{{ $news->newsComments()->count() }}
Make a model for news table name News and another for comments table name Comment. And then in your News model, define a hasMany() relationship method like -
public function comments(){
return $this->hasMany(Comment::class);
}
And then in your controller, retrieve like this-
$newses = News::with('comments')->get();
This will load all news with corresponding all comments. If you want to load single news with all comments then -
$news = News::with('comments')->where('id',$news_id)->first();
To get the comment count, you can count() on relationship like -
$count = News::find($news_id)->comments()->count();
I have post and comments tables where of course comments belongs to posts.
I want to sort posts now with eloquent by amount of comments.
How can i do that in laravel?
I tried to order by date using:
Post::orderBy('created_at', 'DESC')->paginate(6);
i want the same but sorting by comments with are on other database but they are connected in model.
EDIT:
I end up now having this query:
$post = Post::
join('comments', 'comments.post_id' , '=', 'posts.id')
->groupBy('posts.id')
->orderBy(DB::raw('COUNT(posts.id)'))
->paginate(6);
{{ $post->author }} works...
but i cant get for example {{ $post->comment->count()}} from blade. In my previous query (above) it works. dunno why.
This answer makes help.
You should be able to use the Collection's sortBy() and count() methods to do this fairly easily.
$hackathons = Hackathon::with('participants')->get()->sortBy(function($hackathon)
{
return $hackathon->participants->count();
});