我有一个用户模型,它有一组帖子.
我想用他们的帖子返回一组用户数据,除了每个用户的特殊帖子为json;为此,我使用此代码:
$users=User::with('posts')->get();
foreach($users as $user){
$user->posts=$user->posts->except($except_id);
// $user->posts=null;// ->>> also this code does not work
}
return $users;
编辑:
$except_id = $user->golden_post_id;
解决方法:
此代码有效:
$users=User::with('posts')->get();
foreach($users as $user){
$posts=$user->posts->except($except_id);
unset($user->posts);
$user->posts=$post;
}
return $users;