Php 数据保存或更新时,Laravel返回null
我多次使用这个代码部分都没有问题。但是这次$success返回null,我无法理解。当我检查数据库时,我看到用户1已更新。因为它在保存,所以没有可填充的问题。我还尝试了save()函数。我错过了什么?谢谢你的帮助 (Laravel版本5.2.45) 用户模型Php 数据保存或更新时,Laravel返回null,php,laravel,Php,Laravel,我多次使用这个代码部分都没有问题。但是这次$success返回null,我无法理解。当我检查数据库时,我看到用户1已更新。因为它在保存,所以没有可填充的问题。我还尝试了save()函数。我错过了什么?谢谢你的帮助 (Laravel版本5.2.45) 用户模型 <?php namespace App; use App\Presenters\UserPresenter; use App\Services\Logging\UserActivity\Activity; use App\Suppo
<?php
namespace App;
use App\Presenters\UserPresenter;
use App\Services\Logging\UserActivity\Activity;
use App\Support\Authorization\AuthorizationUserTrait;
use App\Support\Enum\UserStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
use CanResetPassword, AuthorizationUserTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $dates = ['last_login', 'birthday'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function items()
{
return $this->hasMany('App\Item');
}
/**
* Always encrypt password when it is updated.
*
* @param $value
* @return string
*/
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
public function gravatar()
{
$hash = hash('md5', strtolower(trim($this->attributes['email'])));
return sprintf("//www.gravatar.com/avatar/%s", $hash);
}
public function isUnconfirmed()
{
return $this->status == UserStatus::UNCONFIRMED;
}
public function isActive()
{
return $this->status == UserStatus::ACTIVE;
}
public function isBanned()
{
return $this->status == UserStatus::BANNED;
}
public function city()
{
return $this->belongsTo(City::class, 'city_id');
}
public function activities()
{
return $this->hasMany(Activity::class, 'user_id');
}
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthpassword()
{
return $this->password;
}
public function getAuthIdentifierName()
{
return $this->getKeyName();
}
public function verification()
{
return $this->hasOne('App\Verification');
}
}
成功插入或更新记录时,laravel会在成功时返回true,失败时返回false,您也可以这样检查
if($user->save()){
//do something when user is save
}else{
// do something wehn user is not update
}
或
我通常这样做是为了确保成功保存
try {
DB::beginTransaction();
// save or update happen in here
DB::commit();
} catch (\Exception $e) {
DB::rollback();
// something happen in here when error
}
为了
是可选的,但我建议它在中间发生任何错误时,它会为您回滚保存的数据。
尝试使用数组更新用户。像这样
$user = User::find(1);
$dataToUpdate['firstname'] = $request->firstname;
$dataToUpdate['lastname'] = $request->lastname;
if($user->update($dataToUpdate))
{
echo "Success";
}
echo "Fail"; // Print "Fail" but update was successful.
您还可以尝试添加:
protected $fillable = ['firstname', 'lastname'];
对于您的User.php模型,我解决了这个问题。有一个特性会覆盖save和update函数
use App\Support\Authorization\AuthorizationUserTrait
谢谢Anuraj,我也试过了,但仍然返回null并转到其他部分你可以分享你的用户模型@GokhanDemirel。。您的表格和可填写表格是否有代码??像这样**受保护的$table='users';受保护的$fillable=['username','password']**我将用户模型添加到我的question@GokhanDemirel我想不出确切的答案。。但您可能需要通过$user=Auth::user()对用户模型进行身份验证;方法并尝试…$user->update()
返回false,但更新成功吗?奇怪!是的,Sanzeeb,这就是为什么它很奇怪。我每次都用成功的方法,从来没有遇到过这样的问题。控制器中还有另一个功能,可以更新用户的电话号码,并且$success可以毫无问题地返回true。相同的代码,一个有效,另一个无效。我已经看过源代码。。。返回NULL是没有选择的(除非您已经跳过了模型更新方法),我遇到了一个类似的问题,$user->save不会返回任何东西(如此正确/错误,没有错误,没有任何东西),这使我无法检查成功/失败。结果表明,协作者创建了一个自定义UserUpdated事件/侦听器,该事件/侦听器覆盖了任何返回。
DB::beginTransaction();
DB::commit();
DB::rollback();
$user = User::find(1);
$dataToUpdate['firstname'] = $request->firstname;
$dataToUpdate['lastname'] = $request->lastname;
if($user->update($dataToUpdate))
{
echo "Success";
}
echo "Fail"; // Print "Fail" but update was successful.
protected $fillable = ['firstname', 'lastname'];
use App\Support\Authorization\AuthorizationUserTrait