简体   繁体   English

如何使用 Laravel 8 中的关系打印各个帖子的所有评论

[英]How to use print all comments for respective posts using relationship in Laravel 8

I am trying to print all comments per post using the relationship,我正在尝试使用该关系打印每个帖子的所有评论,

Error: Property [comments] does not exist on the Eloquent builder instance.错误: Eloquent 构建器实例上不存在属性 [comments]。

Post Model发布 Model

  class WorkTravel extends Model
{
    use HasFactory;

    protected $table = 'work_travel';

    protected $fillable = ['name', 'role', 'location','salary','application_deadline','position','description'];


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

}

Comment Model评论 Model

class CommentWorkTravel extends Model
{
    use HasFactory;

    protected $table = 'comments_for_work_travel';

    protected $fillable = ['message'];

    public function workTravel()
    {
        return $this->belongsTo(WorkTravel::class);
    }

}

Controller Controller

    public function index(){

    $work_travels=WorkTravel::select('id','name','role','application_deadline')
        ->comments()
        ->paginate(6);

    dd($work_travels);
//Error: Call to undefined method \Database\Eloquent\Builder::comments().

Just use the ->comments() insted of ->comments Since all relationships also serve as query builders, you may add further constraints to the relationship query by calling the comments method and continuing to chain conditions onto the query:只需使用 ->comments 的->comments ->comments() insted 由于所有关系也用作查询构建器,因此您可以通过调用 comments 方法并继续将条件链接到查询上来为关系查询添加进一步的约束:
your code should be sth like it你的代码应该是这样的

public function index(){

$work_travels=WorkTravel::select('id','name','role','application_deadline')
    ->comments()
    ->paginate(6);

dd($work_travels);

The error错误

Property [comments] does not exist on the Eloquent builder instance. Eloquent 构建器实例上不存在属性 [comments]。

indicates that you are trying to recieve a value from something that is not a model instance.表示您正在尝试从不是 model 实例的东西中接收值。

See your line看你的线

$work_travels=WorkTravel::select('id','name','role','application_deadline')
    ->comments
    ->paginate(6);

You are selecting a row of type "WorkTravel" and than calling for the property "comments" (without () at the end).您正在选择一行“WorkTravel”类型,而不是调用属性“comments”(末尾没有 ())。 But what you get there is the relation defined in the model, not the result of a query with that relation.但是你得到的是 model 中定义的关系,而不是具有该关系的查询的结果。

It works like this它像这样工作

Model->relationshipmethod()->where('something,'searchvalue')->get()

or或者

Model->relationshipmethod()->all()

if you want to use the rows for a paginated table laravel also offers the paginate method which will recieve a subset of the rows and attach information for getting the previous and next page to the collection returned.如果您想将行用于分页表 laravel 还提供了 paginate 方法,该方法将接收行的子集并将用于获取上一页和下一页的信息附加到返回的集合中。

Model->relationshipmethod()->paginate()

To test that you should modify your statement to use the real model and a relationship method defined in that model.要测试您应该修改您的语句以使用真正的 model 和在该 model 中定义的关系方法。 Not the select method不是 select 方法

$WorkTravelID = 1; // get that from somewhere
$WorkTravel = \App\Models\WorkTravel::find($WorkTravelID);
$work_travels = $WorkTravel->comments()->paginate(6);
dd($work_travels);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM