raviawasti
2 months ago
135
7
Laravel

How to show only user id and name in post , comment and user relationship laravel?

Posted 2 months ago by raviawasti

I have 3 table post, comment, and user:-

      post table field  is id , user_id, title , and content

       Comment table where field are: id , Post_id,user_id, content

       User table where field are: id , name,phone, email, dob, address etc

       Here is route:-

         Route::get('posts', '[email protected]');

public function Allposts() { return Post::with('User','Comment') ->where('status',1)->orderBy('created_at','DESC') ->get(); }

         Post.php model:-
          public function User(){

            return $this->belongsTo('App\User')->select(['id', 'name',]);
          }

          public function Comment(){
          return $this->hasMany(Comment::class,'post_id');
          }


      $post = Post::with('user','subject','Like','Comment')->where('slug',$slug)->first();


      I have 3 table post, comment, and user:-
      post table field  is id , user_id, title , and content
       Comment table where field are: id , Post_id,user_id, content
       User table where field are: id , name,phone, email, dob, address etc

       Here is route:-

         Route::get('posts', '[email protected]');

public function Allposts() { return Post::with('User','Comment') ->where('status',1)->orderBy('created_at','DESC') ->get(); }

         Post.php model:-
          public function User(){

            return $this->belongsTo('App\User')->select(['id', 'name',]);
          }

          public function Comment(){
          return $this->hasMany(Comment::class,'post_id');
          }

My issue when clicking on localhost/posts i am getting all the post data with user detail and comment data with user details , For post i am getting only user id and user name but for comment i am getting all the user details such as id, name , dob, email, But i want user id and name in both posts and comment ..

Please sign in or create an account to participate in this conversation.