我有一个方法可以检查角色是否等于 1,然后发回一些数据。该方法看起来像这样: if($user->role == 1) { $dmca = Dmca::get()->take(5); $data = [ 'status' => 200, 'data' => $dmca ]; return response($data); } else { $dmca = Dmca::where('client', $request->user_id)->get()->take(5); $data = [ 'status' => 200, 'data' => $dmca ]; return response($data); } }在 $user 实例的转储和死亡中,您可以看到角色存在并已设置。但返回时,我收到错误#attributes: array:11 [ "id" => 1 "name" => null "email" => "Grahammorbydev@gmail.com" "model_name" => "man" "subscribed" => 0 "email_verified_at" => null "password" => "$2y$10$yy1Yj.GGez7efEdFdkjaf.RlQS17Zc7QYUANz3RvdE00fVm0f9AYq" "role" => 1 "remember_token" => null "created_at" => "2020-07-05 17:54:38" "updated_at" => "2020-07-05 17:54:38" ] axios返回时出现以下错误"Property [role] does not exist on this collection instance."
1 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
正如错误信息所示
“此集合实例上不存在属性 [角色]。”
它表明这$user
是一个实例Collection
你必须改变
$user = User::where('id', $request->user_id)->get();
对此
$user = User::where('id', $request->user_id)->first();
get()
:返回与查询匹配的模型集合。first()
:返回数据库中找到的第一条记录。如果不存在匹配的模型,则返回 null。
- 1 回答
- 0 关注
- 70 浏览
添加回答
举报
0/150
提交
取消