php Laravel 中的填充方法不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24303003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill method in Laravel not working?
提问by marc_aragones
I am learning how to use Laravel framework but I am having troubles with filling a Model. Here is my code:
我正在学习如何使用 Laravel 框架,但在填充模型时遇到了麻烦。这是我的代码:
The model Event
:
型号Event
:
<?php
class Event extends Eloquent {
//Some functions not used yet
}
And here is the code in the controller:
这是控制器中的代码:
$event = new Event();
$event->fill(array('foo', 'bar'));
print_r($event->attributes);
So, why the print_r
is displaying an empty array?
那么,为什么print_r
显示一个空数组呢?
回答by The Alpha
The attributesis a protected property. Use $obj->getAttributes()method.
该属性是受保护的财产。使用$obj->getAttributes()方法。
Actually. at first you should change the model name from Event
to something else, Laravel
has a Facade
class as Illuminate\Support\Facades\Event
so it could be a problem.
实际上。首先,您应该将模型名称从Event
其他名称更改为其他名称,因为它Laravel
有一个Facade
类,Illuminate\Support\Facades\Event
所以这可能是一个问题。
Regarding the fill
method, you should pass an associative array to fill
method like:
关于该fill
方法,您应该将关联数组传递给fill
方法,例如:
$obj = new MyModel;
$obj->fill(array('fieldname1' => 'value', 'fieldname2' => 'value'));
Also make sure you have a protected $fillable
(check mass assignment) property declared in your Model
with property names that are allowed to be filled. You may also do the same thing when initializing the Model
:
还要确保您在您的属性中声明了一个protected $fillable
(检查质量分配)属性Model
,并使用允许填充的属性名称。你也可以在初始化时做同样的事情Model
:
$properties = array('fieldname1' => 'value', 'fieldname2' => 'value');
$obj = new ModelName($properties);
Finally, call:
最后,调用:
// Instead of attributes
dd($obj->getAttributes());
Because attributes
is a protected property.
因为attributes
是受保护的财产。
回答by JBarlow
Also make sure that the $fillable property is defined in your model class. For example, in your new renamed model:
还要确保在模型类中定义了 $fillable 属性。例如,在您的新重命名模型中:
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['field1', 'field2'];
If you do not have either $fillable or $guarded defined on your Model, fill() won't set any values. This is to prevent the model from mass assignment. See "Mass Assignment" on the Laravel Eloquent docs: http://laravel.com/docs/5.1/eloquent.
如果您没有在模型上定义 $fillable 或 $guarded,fill() 将不会设置任何值。这是为了防止模型进行质量分配。请参阅 Laravel Eloquent 文档中的“批量分配”:http://laravel.com/docs/5.1/eloquent 。
When filling the attributes, make sure to use an associative array:
填充属性时,请确保使用关联数组:
$event->fill(array('field1' => 'val1', 'field2' => 'val2'));
A useful way to debug and check all your values:
调试和检查所有值的有用方法:
//This will var_dump the variable's data and exit the function so no other code is executed
dd($event);
Hope this helps!
希望这可以帮助!
回答by BigfishDim
Use a key/value array in fill:
使用键/值数组填充:
An example:
一个例子:
$book->fill(array(
'title' => 'A title',
'author' => 'An author'
));
回答by Chando
In your model you need to have protected $fillable = ['foo','bar']
;
在你的模型中,你需要有protected $fillable = ['foo','bar']
;
Then you can do:
然后你可以这样做:
$event = new Event(array('foo' => "foo", "bar" => "bar"));
$event->save(); // Save to database and return back the event
dd($event);
The array key needs to be on the $fillable
, as Laravel will effectively call:
数组键需要在 上$fillable
,因为 Laravel 会有效地调用:
foreach ($array as $key => $value){
$event->$key = $value;
}
Alternatively, you can write directly to a column name:
或者,您可以直接写入列名:
$event = new Event();
$event->foo = "foo";
$event->bar = "bar";
$event->save();
dd($event);