Reputation: 11
I am using this code part many times without problem. But this time $success return null and i couldn't figure out. When i check database, i see User 1 is updated. Since it's saving there is no fillable problem. Also i tried save() function too. What am i missing? Thanks for help.
(Laravel version 5.2.45)
$user = User::find(1);
$user->firstname = $request->firstname;
$user->lastname = $request->lastname;
$success= $user->update(); // Database update is successful
dd($success); // But return null
User model
<?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');
}
}
Update Question (New Try):
I tried empty controller with just update function. I still get failure.
UserController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function getEdit()
{
$user = User::find(1);
return view('user.editprofile', compact('user'));
}
public function postEdit(Request $request)
{
$user = User::find(1);
$user->firstname = $request->firstname;
$user->lastname = $request->lastname;
if($user->update())
{
echo "Success";
}
echo "Fail"; // Print "Fail" but update was successful.
}
}
Upvotes: 1
Views: 4307
Reputation: 11
I figured out problem. There is a trait which override save and update function.
use App\Support\Authorization\AuthorizationUserTrait
Upvotes: 0
Reputation: 127
Try update user using array. Like this
$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.
Also you can try to add:
protected $fillable = ['firstname', 'lastname'];
to your User.php Model
Upvotes: 0
Reputation: 1564
I usually did this for make sure is saved successfully
try {
DB::beginTransaction();
// save or update happen in here
DB::commit();
} catch (\Exception $e) {
DB::rollback();
// something happen in here when error
}
for
DB::beginTransaction();
DB::commit();
DB::rollback();
is optional but I recommend it cause when anything bad happen in the middle it will rollback the saved data for you.
Upvotes: 0
Reputation: 157
When successfully inserted or updated records , laravel return true on success and false on failure and also you can check like this.
if($user->save()){
//do something when user is save
}else{
// do something wehn user is not update
}
or
if($user->update()){
//do something when user is update
}else{
// do something wehn user is not update
}
Upvotes: 1