亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“此集合實例上不存在屬性 [角色]。”

“此集合實例上不存在屬性 [角色]。”

PHP
慕容森 2023-09-08 22:00:33
我有一個方法可以檢查角色是否等于 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" => "[email protected]"        "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個贊

正如錯誤信息所示

“此集合實例上不存在屬性 [角色]?!?/p>

它表明這$user是一個實例Collection

你必須改變

$user?=?User::where('id',?$request->user_id)->get();

對此

$user?=?User::where('id',?$request->user_id)->first();
  • get():返回與查詢匹配的模型集合。

  • first():返回數據庫中找到的第一條記錄。如果不存在匹配的模型,則返回 null。

查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 95 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號