php - Laravel - Model::create() works, but is missing attributes

So, I have the following {-code-11}s:
class Recursive extends Model {
public function __construct() {
parent::__construct();
}
// ...
}
class Place extends Recursive {
protected $table = 'places';
protected $fillable = ['name', 'parent_id'];
// ...
}
The following code is used to create a newPlace
:
$place = Place::create([
'name' = 'Second',
'parent_{-code-5}' => 1
]);
This results in the following record in the database:
| Actual | Expected |
As you can see, the only value being set is the Auto-incrementing{-code-5}
column. The 2 columns I'm trying to create are in the{-code-6}
array, and the model is created, but it's not associated correctly.
Has anyone come across this issue before? I know I can use another method, such as
$place = new Place();
$place->name = 'Second';
$place->parent_{-code-5} = 1;
$place->save();
But this isn't the only spot I'm using this code, and I'd prefer to not lose functionality like this.
Edit: Enabling the query log shows the following for the{-code-8}
call:
{-code-9}
Further edit: Enable MySQL log has the same output as above. Following Miken32's suggestion of reverting the{-code-10}
to{-code-11}
works as expected:
array (
'query' => 'insert into `places` (`name`, `parent_{-code-5}`) values (?, ?)',
'bindings' =>
array (
0 => 'Second',
1 => '1'
),
'time' => 1.21,
),
Answer
Answer
Solution:
Checking theIlluminate\Database\Eloquent\Model
class, the constructor looks like this:
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
However, you overrode this in yourRecursive
class:
public function __construct()
{
parent::__construct();
}
The attributes were not being passed to the constructor, so it was not able to successfully build the query. You could remove the constructor since it's not doing anything, or use this instead:
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: npm err! code 1
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.