當我在我的用戶模型中使用此代碼時
public function get_user_by_email($email) {
$data = $this->where('email', $email);
return $data->id;
}
我收到這個錯誤
Property [id] does not exist on the Eloquent builder instance.
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1602
1598▕ if ($key === 'orWhere') {
1599▕ return new HigherOrderBuilderProxy($this, $key);
1600▕ }
1601▕
? 1602▕ throw new Exception("Property [{$key}] does not exist on the Eloquent builder instance.");
1603▕ }
1604▕
1605▕ /**
1606▕ * Dynamically handle calls into the query instance.
1 app/Models/User.php:64
Illuminate\Database\Eloquent\Builder::__get()
2 app/Models/invite.php:21
App\Models\User::get_user_by_email()
請幫助代碼應該作業,我已經用虛擬用戶填充了我的資料庫。為什么我不能從用戶模型中獲取我的用戶 ID。我為此使用了 jetstream
uj5u.com熱心網友回復:
您需要使用first()onEloquent Builder來回傳 ,Model然后才能訪問其屬性。
$data = $this->where('email', $email)->first();
return $data->id;
uj5u.com熱心網友回復:
你可以試試這個方法。
$data = Model::where('email', $email)->pluck('id');
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/402143.html
