Get specific property of a model returns error: Undefined property: Illuminate\Database\Eloquent\Builder::$id
Get specific property of a model returns error: Undefined property: Illuminate\Database\Eloquent\Builder::$id
我想检查我的用户 table 中是否存在模型,然后获取它的 ID 属性 以插入新记录,但得到 未定义 属性 错误。我阅读了文档和一些类似的问题,但它们没有用。有人可以告诉我如何处理吗?
控制器:
$checkExist=Users::where('codeMeli', $codeMeli);
if (!empty($checkExist))
{
$checkOwner=DB::table('user_admin')->whereRaw('userid='.$checkExist->id.' and adminid='.$CurrentUSerID)->get();
if($checkOwner>0)
{
//alert user was added before
}
else
{
$result= DB::table('user_admin')->insert(['userid'=>$checkExist->id,'adminid'=>$CurrentUSerID]);
}
}
错误:
Undefined property: Illuminate\Database\Eloquent\Builder::$id
你应该使用 first
或 get
$checkExist=Users::where('codeMeli', $codeMeli)->first();
我想检查我的用户 table 中是否存在模型,然后获取它的 ID 属性 以插入新记录,但得到 未定义 属性 错误。我阅读了文档和一些类似的问题,但它们没有用。有人可以告诉我如何处理吗?
控制器:
$checkExist=Users::where('codeMeli', $codeMeli);
if (!empty($checkExist))
{
$checkOwner=DB::table('user_admin')->whereRaw('userid='.$checkExist->id.' and adminid='.$CurrentUSerID)->get();
if($checkOwner>0)
{
//alert user was added before
}
else
{
$result= DB::table('user_admin')->insert(['userid'=>$checkExist->id,'adminid'=>$CurrentUSerID]);
}
}
错误:
Undefined property: Illuminate\Database\Eloquent\Builder::$id
你应该使用 first
或 get
$checkExist=Users::where('codeMeli', $codeMeli)->first();