// ClientUser模型
public function devices()
{
return $this->hasMany(UserDevice::class, 'as_user_id');
}
public function last_device(){
$device = UserDevice::where('as_user_id',$this->id)->orderBy('id','desc')->first();
return $device?$device:null ;
}
//控制器:
$user = ClientUser::where("id",$request->auth_user_id)->first();
$last_device = $user? $user->last_device : null;
dd(optional($last_device)->id);
//错误消息:
App \ Models \ Frontend \ ClientUser :: last_device必须返回一个关系实例