我已阅读here(部分:暂时修改属性可见性),我们可以暂时隐藏对象属性。
是否也可以隐藏儿童属性?
例如:
//User model
class User extends \App\AppModel
{
public function articles()
{
return $this->hasMany(Article::class);
}
}
从数组中排除:
$user = User::find($id);
$userData = $user->makeHidden(['last_name', 'articles.*.user_id'])->toArray();
dd($userData); //user_id still present in articles :(
答案 0 :(得分:3)
$userData = User::with('articles')->where('id', $id)->get()->each(function($user){
$user->articles->makeHidden(['user_id']);
})->makeHidden(['last_name'])->toArray();
dd($userData);
答案 1 :(得分:1)
你可以试试像
这样的东西$userData->articles = $user->articles()->get()->each(function($item){
$item->makeHidden(['user_id']);
});
dd($userData);