3

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.

enter image description here

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 => "*"
  ]
}
6
  • 2
    What is the return value of $unit->save(), and what is logged in /storage/logs/laravel.log? Commented Mar 1, 2017 at 10:14
  • Why do you define $name and $description as data members of your class? Problem is there. Commented Mar 1, 2017 at 10:21
  • @veelen, the return value is true. And the log is empty, I emptied it a few minutes ago because it contained about 22000 lines. Nothing is logged right now. Commented Mar 1, 2017 at 10:21
  • Did you tried commenting $name and $description from Model? Commented Mar 1, 2017 at 10:23
  • What is the field type of name and description in your migration? Commented Mar 1, 2017 at 10:23

3 Answers 3

5

Remove this:

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

/**
 * @var string
 */
public $description = NULL;
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain why this is causing the bug? I'm currious ^^
Yeah, what this means is you are setting the values of these attributes directly from the model. So if you save, it is getting these values (NULL) instead of what you are sending. It also affects data retrieval too. If you have saved data in your database and then add these lines back, when you want to retrieve, these values would be null.
1

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.

Comments

0

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'];

5 Comments

There is no $guarded attribute in my model. However, I added protected $primaryKey = 'id'; and tried it again, but it does not work.
Then you should add it ... It is available in your model, because it extends the Illuminate\Database\Eloquent\Model which uses the Concerns\GuardsAttributes Trait.
do you use the artisan command artisan make:model to create a new model?
He already has the $fillable to whitelist the attributes he wants saved.
No i did not used artisan make:model. I think we only need guarded if we use middleware?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.