Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Update not working

Tags:

php

laravel

I have table called admins and model called Admin.

When i try to update the records it fails every time with one field.

$admindetail = Admin::find($id);
$admindetail->fill(['is_delete'=>1])->save();

Above code is not updating record in DB. datatype for is_delete is tinyint.

If i update name field it works with the same code like

$admindetail = Admin::find($id);
$admindetail->fill(['name'=>'abc'])->save();

Can you help me why first code is not working.

like image 936
Saurabh Parekh Avatar asked Jun 07 '17 06:06

Saurabh Parekh


3 Answers

Model use to interact with table so all attributes which you need to store in DB you should need to define in your model as your Admin model should look like

   <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
    protected $fillable = ['name', 'is_delete'];
}

You missing is_delete from your Model code.

like image 194
Ramzan Mahmood Avatar answered Sep 22 '22 06:09

Ramzan Mahmood


Instead of passing value as array, you could try the code below:

$admindetail = Admin::find($id);
if(!$admindetail)
{
  $admindetail->is_delete=1;
   $admindetail->save();
}
like image 20
sathish R Avatar answered Sep 25 '22 06:09

sathish R


You should check your fillable in your model like this:

protected $fillable = [
    'name',
    'is_delete'
];
like image 25
Gilbert Tuazon Avatar answered Sep 23 '22 06:09

Gilbert Tuazon