简体   繁体   English

Laravel 5阵列,模型中带有键保存

[英]Laravel 5 Array with key save in model

Have array which have key & value . 有阵列有关键和价值 The keys name are same database columns name. 键名称是相同的数据库列名称。

Total of keys in an index is greater than 50. 索引中的键总数大于50。

  • So, not possible to save data one by one key like 所以, 不可能像往常一样保存数据

     $user = new User; $user->name = 'John'; .......... ....... .......so on $user->save(); 

Sample array: 样本数组:

Array
(
    [name] => 'Amit',
    [age] => 25,
    [field1] => 'val1',
    ....
  [field33] => 'how'    ,
....
   [field54] => 'sumit'     
)

Now, my question is that how can i save data in an model using easiest process . 现在,我的问题是如何使用最简单的流程在模型中保存数据

Info: 信息:

  • Using laravel 5 使用laravel 5

Try: 尝试:

$Plan=new Plans;
 $Plan->fill( $array);
 $Plan->save();

Note: I know that fill not working as it not defined at fillable variable of that model 注意: 我知道填充不起作用,因为它没有在该模型的可填充变量中定义

The Eloquent model has encapsulated the attributes of the object. Eloquent模型封装了对象的属性。 So you can only alter/set them through the set ( $model->attribute ) or the fill(array $attributes) method. 因此,您只能通过set$model->attribute )或fill(array $attributes)方法更改/设置它们。 All the Eloquent methods that mass assign the attributes is using the method fill() . 质量分配属性的所有Eloquent方法都使用方法fill()

So there are 2 ways to insert new Eloquent based models with an array in the database, one that uses the fill() method and one that doens't. 因此,有两种方法可以在数据库中插入带有数组的基于Eloquent的新模型,一种使用fill()方法,另一种使用dons't。

Method 1: Mass assignment 方法1:批量分配

Add the attribute protected $fillable = [ 'column_a', 'column_b', .. ]; 添加属性protected $fillable = [ 'column_a', 'column_b', .. ]; to your model. 到你的模型。 Then you can use the mass assignment like this. 然后你可以像这样使用质量分配。

$Plan = new Plans;
$Plan->fill( $array );
$Plan->save();

Or you can use: 或者您可以使用:

$plan = Plans::create( $array );

Method 2: QueryBuilder::insert( array ) 方法2:QueryBuilder :: insert(数组)

If you don't have any boot methods registered on creating and created (DB won't trigger these) then you also can use the QueryBuilder to insert the rows in the database. 如果在creatingcreated时没有注册任何boot方法(DB不会触发这些),那么您也可以使用QueryBuilder在数据库中插入行。

// insert one plan
DB::table('plans')->insert([ $array ]);
$plan = Plans::find( DB::getPdo()->lastInsertId() );

// insert multiple plans in one query
$plans = [ $plan_a_arr, $plan_b_arr, .. ];
DB::table('plans')->insert( $plans );

I don't understand why you don't want to define the database columns/array keys in $fillable attribute of your model, as it is a just a one-time task and then you would be able to do this simply: 我不明白为什么你不想在你的模型的$fillable属性中定义数据库列/数组键,因为它只是一次性任务,然后你就可以这样做:

$plan = Plans::create( $array );

Another way, if you have all the data in an associative array with keys matching to the columns in DB table, which you do, is to use a foreach loop: 另一种方法是,如果你拥有一个关联数组中的所有数据,其中的键与数据库表中的列匹配,那么就是使用foreach循环:

$plan = new Plans;
foreach( $array as $key => $value )
{
    $plan->$key = $value;
}
$plan->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM