Laravel中的填充方法不起作用?

时间:2021-01-20 15:22:23

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:

模特活动:

<?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显示一个空数组?

5 个解决方案

#1


24  

The attributes is 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方法,你应该传递一个关联数组来填充方法,如:

$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:

还要确保在模型中声明了受保护的$ fillable(检查质量赋值)属性,其属性名称允许填充。初始化模型时,您也可以执行相同的操作:

$properties = array('fieldname1' => 'value', 'fieldname2' => 'value');
$obj = new ModelName($properties);

Finally, call:

// Instead of attributes
dd($obj->getAttributes());

Because attributes is a protected property.

因为属性是受保护的属性。

#2


9  

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!

希望这可以帮助!

#3


2  

Use a key/value array in fill:

在填充中使用键/值数组:

An example:

$book->fill(array(
    'title'  => 'A title',
    'author' => 'An author'
));

#4


0  

In Laravel 5.x,

在Laravel 5.x中,

When u wanna create a new row:

当你想创建一个新行时:

$model = App\ModelName::create($arr);

When u wanna update an exist row:

当你想更新一个存在的行时:

$model = App\ModelName::find($id);
$model->fill($arr);

All fillable attributes are exist in model variable $fillable.

所有可填充属性都存在于模型变量$ fillable中。

#5


0  

In your model need have protected $fillable = ['foo','bar'];

在你的模型中需要保护$ fillable = ['foo','bar'];

then

$event = new Event(array('foo' => "foo", "bar" => "bar"));
$event->save();// it save to database and return data back
dd($event);

Array key have a name the same on $fillbale cuz it will convert

数组键的名称与$ fillbale相同,因为它将转换

foreach($array as $key => $value){
  $event->$key = $value;
}

or write direct column name

或写直接列名

$event = new Event();
$event->foo = "foo";
$event->bar = "bar";
$event->save();
dd($event);

#1


24  

The attributes is 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方法,你应该传递一个关联数组来填充方法,如:

$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:

还要确保在模型中声明了受保护的$ fillable(检查质量赋值)属性,其属性名称允许填充。初始化模型时,您也可以执行相同的操作:

$properties = array('fieldname1' => 'value', 'fieldname2' => 'value');
$obj = new ModelName($properties);

Finally, call:

// Instead of attributes
dd($obj->getAttributes());

Because attributes is a protected property.

因为属性是受保护的属性。

#2


9  

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!

希望这可以帮助!

#3


2  

Use a key/value array in fill:

在填充中使用键/值数组:

An example:

$book->fill(array(
    'title'  => 'A title',
    'author' => 'An author'
));

#4


0  

In Laravel 5.x,

在Laravel 5.x中,

When u wanna create a new row:

当你想创建一个新行时:

$model = App\ModelName::create($arr);

When u wanna update an exist row:

当你想更新一个存在的行时:

$model = App\ModelName::find($id);
$model->fill($arr);

All fillable attributes are exist in model variable $fillable.

所有可填充属性都存在于模型变量$ fillable中。

#5


0  

In your model need have protected $fillable = ['foo','bar'];

在你的模型中需要保护$ fillable = ['foo','bar'];

then

$event = new Event(array('foo' => "foo", "bar" => "bar"));
$event->save();// it save to database and return data back
dd($event);

Array key have a name the same on $fillbale cuz it will convert

数组键的名称与$ fillbale相同,因为它将转换

foreach($array as $key => $value){
  $event->$key = $value;
}

or write direct column name

或写直接列名

$event = new Event();
$event->foo = "foo";
$event->bar = "bar";
$event->save();
dd($event);