我有一個(gè)方法可以檢查角色是否等于 1,然后發(fā)回一些數(shù)據(jù)。該方法看起來像這樣: 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 實(shí)例的轉(zhuǎn)儲和死亡中,您可以看到角色存在并已設(shè)置。但返回時(shí),我收到錯(cuò)誤#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返回時(shí)出現(xiàn)以下錯(cuò)誤"Property [role] does not exist on this collection instance."
1 回答

慕田峪7331174
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
正如錯(cuò)誤信息所示
“此集合實(shí)例上不存在屬性 [角色]?!?/p>
它表明這$user
是一個(gè)實(shí)例Collection
你必須改變
$user?=?User::where('id',?$request->user_id)->get();
對此
$user?=?User::where('id',?$request->user_id)->first();
get()
:返回與查詢匹配的模型集合。first()
:返回?cái)?shù)據(jù)庫中找到的第一條記錄。如果不存在匹配的模型,則返回 null。
- 1 回答
- 0 關(guān)注
- 101 瀏覽
添加回答
舉報(bào)
0/150
提交
取消