【问题标题】:Property [id] does not exist on the Eloquent builder instanceEloquent 构建器实例上不存在属性 [id]
【发布时间】:2021-01-06 04:07:28
【问题描述】:

我正在尝试获得评论,它在 laravel api 中的回复,我尝试了以下方式并遇到了这个错误,我该如何解决? 这是我的控制器:

 public function postSinglePage(Request $request)
{
    $post_id=$request->get('post_id');
    $post = Post::where('id',$post_id)->where('status', '=', 1)->get();
    return response([
        "posts" => GetPostsSinglePageData::collection($post),

    ]);


}

这是我要发布的资源集合,它是 cmets:

public function toArray($request)
{

    return [
        'post_id' => $this->id,
        "name" => $this->post_title,
        "excerpt" => $this->post_excerpt,
        "thumbnail" => $this->getFirstMediaUrl("images"),
        "logo" => $this->getFirstMediaUrl("images"),
        'comment_count'=>getCommentCount($this->id),
        'comments'=> new CommentCollection(Comment::select('*')->where('post_id',$this->id)),
        "like" => DB::table("like_tb")->select('like_dislike')
            ->where("type_like",'=','post')
            ->where('related_id',$this->id)
            ->get(),
        "share" => DB::table("share_tb")->select("share_number")
            ->where('type','=','post')
            ->where("related_id",$this->id)
            ->get(),

    ];
}

这是评论集:

public function toArray($request)
{


    return [
        'commentId'=>$this->id,
        'courseId'=>$this->post_id,
        'commentDate'=>$this->date,
        'commentText'=>$this->body,
        'userId'=>$this->user_id,
        'replays'=>ReplyCommentCollection::collection(\App\Models\Comment\ReplyComment::where('comment_id',$this->id)->get()),
        'users'=>UserInfoCollection::collection(User::where('id',$this->user_id)->get()),
    ];
}

【问题讨论】:

  • Comment::select('*')->where('post_id',$this->id) ... 那是一个构建器,您从未执行过查询,您必须调用firstget 等来执行查询... 旁注:使用$request->input(...) 而不是 $request->get(...)
  • 是的!你说得对,我应该先执行我的查询。

标签: laravel api laravel-api


【解决方案1】:

$this->id 在内部其他函数中不起作用,您必须这样编写:

  public function toArray($request)
    {
        $comments=Comment::select('*')->where('post_id',$this->id)->get();
        $commentCollection= CommentCollection::collection($comments);
        return [
            'post_id' => $this->id,
            "name" => $this->post_title,
            "excerpt" => $this->post_excerpt,
            "thumbnail" => $this->getFirstMediaUrl("images"),
            "logo" => $this->getFirstMediaUrl("images"),
            'comment_count'=>getCommentCount($this->id),
            'comments'=> $commentCollection,
            "like" => DB::table("like_tb")->select('like_dislike')
                ->where("type_like",'=','post')
                ->where('related_id',$this->id)
                ->get(),
            "share" => DB::table("share_tb")->select("share_number")
                ->where('type','=','post')
                ->where("related_id",$this->id)
                ->get(),

        ];
    }

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2022-01-15
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多