我是 Laravel 的新手,遇到以下错误,
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
这是造成问题的代码
Route::get('/insert', function(){
$user = User::findOrFail(1);
$address = new Address(['name' => '2250 nw 59st Seattle, WA 98107']);
$user->address()->save( $address);
});
问题是什么,我该如何解决。
最佳答案
如果您查看 5.4 源代码,您会发现出现此错误是因为您已将 $fillable
属性定义为字符串,例如:
protected $fillable = 'name';
但它应该是一个数组:
protected $fillable = ['name'];
关于php - 给出的错误 : array_flip() expects parameter 1 to be array, 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101395/