Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: array_flip() expects parameter 1 to be array, string given

Tags:

php

laravel

I am new to Laravel, getting the following error,

array_flip() expects parameter 1 to be array, string given

in GuardsAttributes.php line 188
at HandleExceptions->handleError(2, 'array_flip() expects parameter 1 to be array, string given', '/Users/aaronmk2/Desktop/CodingDojo/php/onetoone/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php', 188, array('attributes' => array('name' => '2250 nw 59st Seattle, WA 98107')))
at array_flip('name') in GuardsAttributes.php line 188
at Model->fillableFromArray(array('name' => '2250 nw 59st Seattle, WA 98107')) in Model.php line 216
at Model->fill(array('name' => '2250 nw 59st Seattle, WA 98107')) in Model.php line 145
at Model->__construct(array('name' => '2250 nw 59st Seattle, WA 98107')) in web.php line 24

Here is the code that is creating the problem

Route::get('/insert', function(){
    $user = User::findOrFail(1);

    $address = new Address(['name' => '2250 nw 59st Seattle, WA 98107']);

    $user->address()->save( $address);
});

What is the issue and how can I fix it.

like image 757
Aaron Avatar asked Aug 12 '26 15:08

Aaron


1 Answers

If you'll look into 5.4 source code, you'll see that this error occurs because you have defined $fillable property as a string, like:

protected $fillable = 'name';

But it should be an array:

protected $fillable = ['name'];
like image 97
Alexey Mezenin Avatar answered Aug 14 '26 11:08

Alexey Mezenin