Farshad
Farshad

Reputation: 2000

Laravel - how to access the relationship through laravel api

I have a Laravel and Vue.js app that I am sending the comments through an API to posts page. Now I want to get the comments which are for that post through that API and here is what I have tried:

commentController

public function index(){
    $comment = Comment::with('post')->get();
    return new CommentResource($comment);
}

commentmodel

public function post(){
    return $this->belongsTo(Post::class);
}

postmodel

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

and here is my Api route

Route::resource('comment','CommentController');

and finally here is what I get when I hit

http://localhost:8000/api/comment

which is all of the comments not the comments that belong to the specific post

{
    "data": [{
        "id": 33,
        "body": "a",
        "user_id": 1,
        "user_email": "a",
        "user_name": "a",
        "status": 0,
        "post_id": 9,
        "created_at": "2019-03-25 08:50:55",
        "updated_at": "2019-03-25 08:50:55",
        "post": null
    }, {
        "id": 32,
        "body": "a",
        "user_id": 1,
        "user_email": "a",
        "user_name": "a",
        "status": 0,
        "post_id": 9,
        "created_at": "2019-03-25 08:50:55",
        "updated_at": "2019-03-25 08:50:55",
        "post": null
    }, {
        "id": 31,
        "body": "a",
        "user_id": 1,
        "user_email": "a",
        "user_name": "a",
        "status": 0,
        "post_id": 9,
        "created_at": "2019-03-25 08:50:55",
        "updated_at": "2019-03-25 08:50:55",
        "post": null
    }, {
        "id": 30,
        "body": "a",
        "user_id": 1,
        "user_email": "a",
        "user_name": "a",
        "status": 0,
        "post_id": 1,
        "created_at": "2019-03-25 08:50:55",
        "updated_at": "2019-03-25 08:50:55",
        "post": null
    }]
}

Any idea how to send just the related comment to the specific post?

Upvotes: 0

Views: 701

Answers (4)

Sandeep Sudhakaran
Sandeep Sudhakaran

Reputation: 1092

Try the below code,

I changed your controller index() function to

use NorAm\UserManagement\Http\Requests\Request;

class myController{

   public function index(Request $request){
      $postId = $request->postId; // this should pass through you API. 
      $comment = Comment::with('post')->where('post_id',$postId)->get(); 
      return new CommentResource($comment);
   }

}

Upvotes: 1

Karan Sadana
Karan Sadana

Reputation: 1373

Am assuming you have post_id in your comments table. So for getting comments of the related to the particular post, you need to pass post_id(for which post you need comment) into your where. So the query will be:- Comment::with('post)->where('post_id',$postId)->get(); for fetching comments with related post.

Another way with your Post Model:-

And if you are fetching post and you need related comments with the post(your requirement). then in your model change your comments method with

public function comments(){ return $this->belongsToMany(Comment::class); } Post::with(comments)->get()

with this you can get post as well as comments

Upvotes: 1

Marco Bonomo
Marco Bonomo

Reputation: 11

In CommentResource.php you should return the relationship when available

return [
...
'post' => new PostResource($this->whenLoaded('post')),
...
];

Upvotes: 1

namelivia
namelivia

Reputation: 2735

Your relationshipts seem to be correct, should be able to get all the Comments on a json structure with it's related post nested like:

public function index()
{
    return response()->json(Comment::with('post')->get());
}

Maybe you should check your database table structure and foreign keys

Upvotes: 1

Related Questions