Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

industriousagency's avatar

Eloquent Create Method - Always inserts blank entries.

Has anyone experienced an issue where Laravel refuses to insert data when using the Model::create() method?

I know it's nothing to do with the fields not being mass-assignable (http://stackoverflow.com/questions/15659708/laravel-4-eloquent-mass-assignment-creates-empty-records-after-update), because I've got the $fillable property set correctly on the model. I can also use the fill method (and save) to insert data on that model, it's just the create method that doesn't seem to insert anything..

To verify it's not to do with being mass assignable, I've also done:

Client::unguard();
Client::create($data);
Client::reguard();

Which still doesn't work..

In Eloquent/Model.php:

public static function create(array $attributes)
{
 $model = new static($attributes);

 $model->save();

 return $model;
}

If I output the first argument, the attributes are there, it just seems to be when the new class is instantiated it doesn't fill the properties for some reason..

This is actually on L5, but I have had the same on L4 as well.

Thanks in advance!

0 likes
10 replies
Marwelln's avatar

Show us a dump of your $data and your $fillable property in your model.

industriousagency's avatar

Sorry, only just come back to this...

Surely if I'm doing Eloquent::unguard(), the $fillable property shouldn't matter?

Here's an example of a project I'm working on at the moment:

Eloquent::unguard();

  $centre = Centre::create([
   'name'   => 'Test'
  ]);

Eloquent::reguard();

Then in my Centre model, I've got:

/**
  * @var array
  */
 protected $fillable = [
  'name', 'territory', 'address_1', 'address_2', 'address_3', 'town_city', 'postal_code', 'country_id', 'phone', 'email'
 ];

I must be doing something wrong, as I come across this issue in every Laravel project I work on.

zefman's avatar

So does the above code create an entry in the db with literally nothing in it accept an id?

industriousagency's avatar
Level 2

Ahhh shit. Just got it.

I'm extending a BaseModel, in that I've got a constructor which I'm not accepting any arguments

/**
 * Constructor
 *
 */
public function __construct()
{
 parent::__construct();

 Carbon::setToStringFormat('d-m-Y H:i:s');
}

When it should be:

/**
 * Constructor
 *
 * @param array $attributes
 */
public function __construct(array $attributes = array())
{
 parent::__construct($attributes);

 Carbon::setToStringFormat('d-m-Y H:i:s');
}

Your comment somehow made me realise what the problem might be, so thank you.

6 likes
zefman's avatar

Ah ha nice one! Was about to suggest dumping the sql queries but no need now :)

1 like
mm256's avatar

<?php

class Comment extends Eloquent{

   /**
 * The database table used by the model.
 *
 * @var string
 */
protected $table ='comment';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = array('picture_id', 'user_id', 'content' );

}

same problem here.. it doesnt insert the values i dd() before i use create

1 like
brian-lamb-software-engineer's avatar

So im having this issue, also using a constructor, which i think it has less to do with that, than with the fact that im trying to mass save.

When mass saving an array, only insert() is working, but i get no timestamps. So i sought to use create(), which is causing this topic-error. When moving to create, it forced me to use the fillable, which i DO have set now.

Only the timestamps and autoincrement id are being populated, the rest made me set nullable, or a default value beacuse its adding them as null. At that, only one record is being saved, i am pushing 4 records.

I used create because i dont want to iterate queries, because there is potentially thousands of elements, i am looping in php and passing the data array.

The solution here didnt work, because i was already passing the attributes (like this parent::__construct($this->attributes);). Although the code example might be wrong, i dont think it should be parent::__construct($attributes);. I did try both ways just incase, niether solved it.

Note, im not extending a base model like the OP, but I am simply using a constructor. Unguard didnt help either.

shiroamada's avatar

Another funny mistake I make is

$data = [
$id, 1,
$user_id ,2,
]; // use comma instead of =>
Client::create($data);

Logic error..

Please or to participate in this conversation.