<已解决>如何在查询时或查询结果集中设置预加载关联关系的$visible属性
想请教一下: 如何在查询时或查询结果集中设置预加载关联关系的$visible
属性
问题描述:
假设有以下两个模型
class Author extends Model
{
public function articles()
{
return $this->hasMany(Article::class);
}
}
class Article extends Model
{
//假设 有id, author_id, content, view_count, created_at, updated_at等字段
public function author()
{
return $this->belongTo(Author::class);
}
}
在按照author进行分页时 想要指定预加载关联关系的visible 要怎么设置? 只能遍历一遍吗?
$authors=Author::with("article")->paginate();
foreach($authors as $author){
$author->articles->makeVisible(["id","view_count"]);
}
- 上面举的
author
和article
的例子可能并不是很恰当, 主要是想说明大概的问题
推荐文章: