简体   繁体   中英

Laravel - Model is saved with empty attribute value (NULL) to database

I try to save my model to the database. The model is saved into the database, but the values for the attributes are not set and stay empty.

在此处输入图片说明

It is a small model with only a few properties:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Unit extends Model
{
    public $fillable = ['name', 'description', 'created_at', 'updated_at'];

    protected $table = 'units';

    // Allowed fields from forms
    // protected $guarded = ['ic'];

    /**
     * @var string
     */
    public $name = NULL;

    /**
     * @var string
     */
    public $description = NULL;
}

Controllers store function:

/**
 *
 * @param Request $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate(request(), [
        'name' => 'required|max:80|regex:/^[\pL\s\-]+$/u',    //regex: only allows letters, hyphens and spaces explicitly',
        'description' => 'required',
        'image' => 'required|image',
    ]);

    $unit = new Unit();

    $unit->description = $request->description;
    $unit->name = $request->name;

    echo $unit->description . PHP_EOL;  //Outputs "example description"
    echo $unit->name . PHP_EOL;   //Outputs: "example name"

    $unit->save();
    ...

I am confused, it should work. What am I doing wrong?

UPDATE:

The object $unit after save() :

Unit {#190 ▼
  +fillable: array:2 [▼
    0 => "name"
    1 => "description"
  ]
  #table: "units"
  +name: "example name"
  +description: "example description"
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: true
  #attributes: array:3 [▼
    "updated_at" => "2017-03-01 10:18:59"
    "created_at" => "2017-03-01 10:18:59"
    "id" => 27
  ]
  #original: array:3 [▼
    "updated_at" => "2017-03-01 10:18:59"
    "created_at" => "2017-03-01 10:18:59"
    "id" => 27
  ]
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

Remove this:

   /**
 * @var string
 */
public $name = NULL;

/**
 * @var string
 */
public $description = NULL;

As already mentioned you have to remove both attributes from your model class. This is because when these attributes are defined there won't be the magic __set() run and thus the $obj->attributes[] won't be populated with values.

Try to uncomment the $guarded attribute and guard the id .

protected $primaryKey = 'id';

I do assume that it has something to do with the following attribute:

#guarded: array:1 [▼
    0 => "*"
]

Which probably means everything is guarded.

Update

How the guarded Attribute should look like.

protected $guarded = ['id'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM