I have a has many relation between models: post and category in laravel application. I've defined these relations as:
public function category() {
return $this->belongsTo('artSite\category');
}
public function posts() {
return $this->hasMany('artSite\post');
}
Now I'm trying retrieve posts belonging to the particular category which are derived in http request:
Route::get('posts/categories/{categoryName}','postsViewController#showPostGivenCategory')
Below I show my controller function (it does work fine!):
public function showPostGivenCategory($categoryName) {
$category = category::where('category_name','=',$categoryName)-first();
$posts = category::find($category->id)->posts;
return view('pages.homePage')->with('categories',$categories)with('posts',$posts);
}
In this solution I'm creating 2 queries. Is any possible way to create 1 query to retrieve posts of particular category in has many relation?
Something like that doesn't work:
$posts = category::where('category_name','=',$categoryName)->posts;
Could someone help me with this problem? I would be very grateful, greetings.
we can get rid of the second line :
$posts = Category::find($category->id)->posts;
So You can say :
$posts = Category::where('category_name','=',$categoryName)->first()->posts;
Related
I have two tables named posts and categories. these two tables have one-to-many relationship and I wrote them in models like this:
class Post extends Model
{
...
public function category()
{
return $this->belongsTo(Category::class, 'category_id', 'id');
}
...
}
class Category extends Model
{
...
public function posts()
{
return $this->hasMany(Post::class, 'category_id', 'id');
}
...
}
This is work fine when I use $post = Post::find($id) in controller and retrieve one post and use $post->category to get category of this post. but what if I want to retrieve all posts and their categories together?
I mean, let's assume that I have 10 posts in DataBase and I have $posts = Post::get(); to get all posts. now, how I can get categories of each post?
One way is loop! for example:
foreach($posts as $post) {
$post['category'] = $post->category;
}
return response()->json($posts);
Yeah! I want to response in Json and I forgot to say earlier, sorry.
Is there better way to do this? I searched and I got nothing by my search. I'll be appreciated if anyone response to my problem. :)
The easiest way to include categories in each post model with json, is simply including it using with() and it will automatically transform it.
return Post::with('category')->get();
In Laravel you do not have to wrap models or collections in response->json(); it will automatically do that.
i have laravel models
Category: id,name
public function posts(){
return $this->hasMany(PostCategory::class,'category_id','id');
}
PostCategory : post_id, category_id
public function post(){
return $this->belongsTo(Post::class,'post_id');
}
POST: id, ..so on
public function solutions(){
return $this->hasMany(PostSolution::class,'post_id','id');
}
I need to get count of all posts fall under a category and also solutions under one category.. there is no direct relation of category and solution so how to get count of solutions in one category.
$categories = Category::withCount('posts')->get();
I think use hasManyThrough relation
// Category Class
public function solutions()
{
return $this->hasManyThrough(PostSolution::class, Post::class);
}
// Then get the data in the same old manner
$categories = Category::->withCount('posts')->get();
I hope this will help
Is it possible to make a multi level relation query with Eloquent on a deeper level than 1 level? My tables look like this :
post_comments-> id|comment|post_id|user_id|...
post_comment_replies-> id|reply|post_comment_id|user_id|...
users-> id|name|....
user_data-> id|avatar|...
And so I want to ask is it possible to get the Comments for a Post with all the Replies and the User Data for the person who replied to a comment in 1 query with Eloquent.
This is how my Comments Model looks like:
class PostComment extends Model{
public function replies(){
return $this->hasMany(PostCommentAwnsers::class);
}
public function user() {
return $this->belongsTo(User::class);
}
public function userInfo(){
return $this->belongsTo(UserInfo::class,'user_id','user_id');
}}
public function index($id){
$posts = Post::find($id);
$postComments = PostComment::where('post_id','=',$id)->paginate(5);
return view('post.show',[
'post' => $post,
'postComments' =>$postComments
]);
}
And as I get all the user data for a Comment I want to get all the user data for the person who replied.
I am really sorry if this has been awnsered or documented somewhere else but I just can't seem to find the exact solution to this problem.
You should look for eager loading : https://laravel.com/docs/5.3/eloquent-relationships#eager-loading
if you want to get all posts and their comments :
$posts = Post::with('comment')->get();
and if you want all posts with comments and replies of the comments :
$posts = Post::with('comment.reply')->get();
I'm new to laravel and I'm using Laravel 5.2 I want to have all the comments of a particular post of a user. My tables are like :
User
-----
id
public function posts() {
$this->hasMany( 'App\Posts' )
}
Post
------
id
user_id
public function user() {
$this->belongsTo( 'App\User' )
}
public function comments() {
$this->hasMany( 'App\Comment' )
}
Comments
----------
id
post_id
public function post() {
$this->belongsTo( 'App\Post' )
}
Now what I want is to get all the post for a perticular user say the logged in user. I can get all the comments by the long way like :
$comments = [];
foreach( Auth::user()->posts as $post) {
foreach( $post->comments as $comment )
{
$comments[] = $comment->title
}
}
But I was wondering how to get the comments without making these loops using the relations only. Thanks in advance.
You can double the relationship from the existing user object:
$posts = \Auth::user()->posts()->with('comments')->get();
It should work for all levels.
In my project I have Organizations with Clients with a link table to Therapists, all multiple, and this works:
Organization::find(1)->clients()->with('therapists')->get();
so your case should work too. Let me know.
Essentially you want to eager load your data using with() and dot notation like so:
$user = User::with('posts.comments')->find(Auth::user()->id);
If you just want the comments, you can do:
$user = User::with('comments')->find(Auth::user()->id);
$comments = $user->comments();
Try to get all post's by a user in application and show in a table with corresponding column , i tried with this way but showing exception : " Trying to get property of non-object " , how to solve it ?
public function usersPost(){
$uid = \Auth::user()->id;
$posts = DB::table('posts')->('user_id',$uid);
}
Here two table posts and users and user_id is foreign key of posts table .
Laravel allows you to relate models to each other. For instance, you can relate your Post model to your User model like this.
class User extends Eloquent {
public function posts() {
return $this->hasMany('Post');
}
}
After specifying that the User has many posts through a One-to-Many relationship, you can then simply call all of your user's posts.
$posts = User::find(user_id)->posts;
public function usersPost() {
$posts = DB::table('posts')->where('user_id', auth()->id())->get();
}
You need a where clause like this:
$posts = DB::table('posts')->where('user_id', $uid)->get();