我的模型用户有一些关系:配置文件,激活等等.在我的控制器上我有这样一个:
$user = Auth::user();
$user->load(['profile', 'activities']);
在我的刀片上被删除的结果是,我的变量$user与我的关系和Auth::user()数据仍然只是user表数据;
我有一个布局layout.blade.php
和一个视图index.blade.php
来扩展我的布局;
在我的索引中,我想使用关系数据,而在我的布局中,我只想要用户数据;但是,我总是从控制器加载关系。
发布于 2022-04-12 10:09:04
如果加载任何关系,laravel将自动在用户对象中添加该关系。
如果您不想这样,并且只想要用户,那么在加载任何关系之前,您可以先将对象复制到另一个变量。
第二个选项,您可以再次获得用户。
发布于 2022-04-12 10:30:52
如果您想拥有相同的对象,但没有这种关系,您可以有多个选择:
$user->unsetRelation('profile');//unset specific relationship on the $user Object
$user->unsetRelations();//unset all relationships on the $user Object
$userWithoutRelationships = $user->withoutRelations();//clone the current Object but without relationships
https://stackoverflow.com/questions/71840704
复制相似问题