🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
关联一对多中 指定字段 ``` public function comment(){ return $this->hasMany(Comment::class,'article_id','id'); } ``` 使用with需要指定字段需要加上 关联中的主键和外键 就上面方法的 'article_id','id'才行 ~~~ $data=$article->with(['comment'=>function($query){ $query->select(['id', 'comment','article_id']); }])->get()->toArray(); ~~~ 其他写法不使用闭包 > `$data=$article->with('comment:id,comment,article_id')->get()->toArray();` 查询内容 ~~~ { "id": 1, "title": "et", "author": "948", "desc": "Corporis sit necessitatibus eveniet similique ut qui a.", "content": "Exercitationem ipsam eum quo magnam magni. Culpa omnis sit saepe nihil exercitationem. Accusamus optio assumenda ratione quam et aspernatur aut.", "created_at": "1972-02-03 23:13:39", "updated_at": "2009-11-20 07:39:33", "comment": [ { "id": 1, "comment": "颠三倒四", "article_id": 1 } ] } ~~~