💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
关联一对多中 指定字段 ``` 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 } ] } ~~~