【问题标题】:Laravel - how to access the relationship through laravel apiLaravel - 如何通过 laravel api 访问关系
【发布时间】:2023-03-21 01:06:01
【问题描述】:

我有一个 Laravel 和 Vue.js 应用程序,我通过 API 将 cmets 发送到帖子页面。现在我想通过该 API 获取该帖子的 cmets,这是我尝试过的:

commentController

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

评论模型

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

后模型

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

这是我的 Api 路由

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

最后这是我点击时得到的结果

http://localhost:8000/api/comment

这是所有的 cmets,而不是属于特定帖子的 cmets

{
    "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
    }]
}

知道如何将相关评论发送到特定帖子吗?

【问题讨论】:

    标签: php laravel vue.js


    【解决方案1】:

    试试下面的代码,

    我把你的控制器index()函数改成了

    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);
       }
    
    }
    

    【讨论】:

    • 未定义变量:postId
    • 我应该在哪里以及如何定义帖子 ID 我的意思是我应该定义它?
    • 您想要特定帖子的所有评论,对吗?所以你应该有一个 postId。
    • 是的,这就是我的问题,如何访问帖子的 show 方法并获取 cmets 索引中的特定帖子 ID 以传递给 where 查询
    • 试试dd($request->all()) 这会给你来自你的ajax的请求。您应该在 API 调用中传递 postId。您可以从控制器访问该参数,例如$request->paramKey。请查看编辑后的答案
    【解决方案2】:

    您的关系似乎是正确的,应该能够获取 json 结构上的所有评论,其相关帖子嵌套如下:

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

    也许你应该检查你的数据库表结构和外键

    【讨论】:

    • 再次使用您的代码,我得到了所有的 cmets,并且我的评论表上有 post_id,我认为不把它称为关系很好,我猜 laravel 检测到它
    • 如果您的 comments 表上有正确的指向 post_id 外键,那么一切都应该开箱即用。很奇怪。
    【解决方案3】:

    在 CommentResource.php 中,您应该在可用时返回关系

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

    【讨论】:

      【解决方案4】:

      假设您的 cmets 表中有 post_id。因此,为了获取与特定帖子相关的 cmets,您需要将 post_id(您需要评论的帖子)传递到您的位置。 所以查询将是: - Comment::with('post)->where('post_id',$postId)->get(); 用于获取相关帖子的 cmets。

      你的帖子模型的另一种方式:-

      如果您正在获取帖子并且您需要与帖子相关的 cmets(您的要求)。 然后 在您的模型中使用

      更改您的 cmets 方法

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

      有了这个,你可以得到 post 以及 cmets

      【讨论】:

      • sandana 如何访问帖子的 show 方法并获取 cmets 索引中的特定帖子 id 以传递给 where 查询
      • @Farshad 如果您要获取所有帖子,那么您不需要 post_id 只需在您的帖子模型更改方法中使用 cmets hasManybelongToMany 并使用 Post::with(comments)->get(); 获得所有结果,如果您愿意要获得特定的帖子 cmets,那么您必须知道您想要哪个帖子 cmets,为此,您必须知道post_id
      • 不看这个链接localhost:8000/blog/8它是一个特定的帖子,我想显示这个帖子的所有cmets
      • @Farshad 8 是您的 post_id。获取该ID。如果您不知道您的参数 (post_id) 名称,那么您可以通过dd($request) 获取参数名称。假设您的请求参数名称是post_id,您可以使用`$postID = $request->post_id;`
      • 正如我所说,在您需要特定帖子的帖子 ID 之前。所以你的路线是`localhost:8000/api/comment`你没有传递任何post_id那么你怎么能得到post_id。你需要先传递 id 。使用这条路线 `localhost:8000/blog/8`,您可以获取 post_id,因为您正在传递帖子 ID。
      猜你喜欢
      • 1970-01-01
      • 2014-05-15
      • 2023-03-17
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多