我正在使用get()方法从数据库中检索模型。当我想使用保存按钮修改字段时,我收到该错误:调用未定义的方法 Illuminate''Database''Eloquent''Collection::save();
这是我的代码:
public static function authenticate($token='randomToken'){
$u = User::where('token',$token)// $token)
->get(['token']);
if ($u->count()==1){//User is authenticated
$u->token = User::getGUID();
$u->save();
return true;
}
else {
return false;
}
}
public static function getGUID()
{
if (function_exists('com_create_guid')) {
return com_create_guid();
} else {
mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$uuid = substr($charid, 0, 8)
.substr($charid, 8, 4)
.substr($charid, 12, 4)
.substr($charid, 16, 4)
.substr($charid, 20, 12);
return $uuid;
}
}
将
查询更改为
$u = User::where('token',$token)// $token)
->first(['token']);
Get 方法为您提供了一个对象数组,因此无法像您正在执行的那样直接对其进行更新/保存。 但是使用 first() 它只给你一个数据库中的记录,所以保存或更新它要容易得多。