我们如何使用雄辩的ORM在Laravel中执行批量数据库插入?
我想在Laravel中完成此操作:https ://stackoverflow.com/a/10615821/600516, 但出现以下错误。
SQLSTATE [HY093]:无效的参数编号:命名和位置参数混合。
我们如何使用雄辩的ORM在Laravel中执行批量数据库插入?
我想在Laravel中完成此操作:https ://stackoverflow.com/a/10615821/600516, 但出现以下错误。
SQLSTATE [HY093]:无效的参数编号:命名和位置参数混合。
Answers:
您可以使用Eloquent::insert()
。
例如:
$data = array(
array('name'=>'Coder 1', 'rep'=>'4096'),
array('name'=>'Coder 2', 'rep'=>'2048'),
//...
);
Coder::insert($data);
Eloquent
实际上并没有涉及。它只是代理对Query\Builder@insert()
方法的调用。用Eloquent无法有效地插入多行,也没有提供任何用于批量插入的方法。
我们可以更新GTF答案以轻松更新时间戳
$data = array(
array(
'name'=>'Coder 1', 'rep'=>'4096',
'created_at'=>date('Y-m-d H:i:s'),
'modified_at'=> date('Y-m-d H:i:s')
),
array(
'name'=>'Coder 2', 'rep'=>'2048',
'created_at'=>date('Y-m-d H:i:s'),
'modified_at'=> date('Y-m-d H:i:s')
),
//...
);
Coder::insert($data);
更新:为简化日期,我们可以按照@Pedro Moreira的建议使用碳纤维
$now = Carbon::now('utc')->toDateTimeString();
$data = array(
array(
'name'=>'Coder 1', 'rep'=>'4096',
'created_at'=> $now,
'modified_at'=> $now
),
array(
'name'=>'Coder 2', 'rep'=>'2048',
'created_at'=> $now,
'modified_at'=> $now
),
//...
);
Coder::insert($data);
UPDATE2:对于laravel 5,使用updated_at
代替modified_at
$now = Carbon::now('utc')->toDateTimeString();
$data = array(
array(
'name'=>'Coder 1', 'rep'=>'4096',
'created_at'=> $now,
'updated_at'=> $now
),
array(
'name'=>'Coder 2', 'rep'=>'2048',
'created_at'=> $now,
'updated_at'=> $now
),
//...
);
Coder::insert($data);
$now
变量:$now = Carbon::now('utc')->toDateTimeString();
。然后只需'created_at' => $now, 'updated_at' => $now
用于每次插入。
Carbon
在这种情况下最需要什么?这有什么错date("Y-m-d H:i:s")
?
对于正在阅读本文的人,请查看createMany()
方法。
/**
* Create a Collection of new instances of the related model.
*
* @param array $records
* @return \Illuminate\Database\Eloquent\Collection
*/
public function createMany(array $records)
{
$instances = $this->related->newCollection();
foreach ($records as $record) {
$instances->push($this->create($record));
}
return $instances;
}
Model::createMany()
。
这是您以更有口才的方式做到的方式,
$allintests = [];
foreach($intersts as $item){ //$intersts array contains input data
$intestcat = new User_Category();
$intestcat->memberid = $item->memberid;
$intestcat->catid= $item->catid;
$allintests[] = $intestcat->attributesToArray();
}
User_Category::insert($allintests);
我搜索了很多遍,最后使用timestamps
如下的自定义:
$now = Carbon::now()->toDateTimeString();
Model::insert([
['name'=>'Foo', 'created_at'=>$now, 'updated_at'=>$now],
['name'=>'Bar', 'created_at'=>$now, 'updated_at'=>$now],
['name'=>'Baz', 'created_at'=>$now, 'updated_at'=>$now],
..................................
]);
从Laravel 5.7开始,Illuminate\Database\Query\Builder
您可以使用insertUsing方法。
$query = [];
foreach($oXML->results->item->item as $oEntry){
$date = date("Y-m-d H:i:s")
$query[] = "('{$oEntry->firstname}', '{$oEntry->lastname}', '{$date}')";
}
Builder::insertUsing(['first_name', 'last_name', 'date_added'], implode(', ', $query));
$start_date = date('Y-m-d h:m:s');
$end_date = date('Y-m-d h:m:s', strtotime($start_date . "+".$userSubscription['duration']." months") );
$user_subscription_array = array(
array(
'user_id' => $request->input('user_id'),
'user_subscription_plan_id' => $request->input('subscription_plan_id'),
'name' => $userSubscription['name'],
'description' => $userSubscription['description'],
'duration' => $userSubscription['duration'],
'start_datetime' => $start_date,
'end_datetime' => $end_date,
'amount' => $userSubscription['amount'],
'invoice_id' => '',
'transection_datetime' => '',
'created_by' => '1',
'status_id' => '1', ),
array(
'user_id' => $request->input('user_id'),
'user_subscription_plan_id' => $request->input('subscription_plan_id'),
'name' => $userSubscription['name'],
'description' => $userSubscription['description'],
'duration' => $userSubscription['duration'],
'start_datetime' => $start_date,
'end_datetime' => $end_date,
'amount' => $userSubscription['amount'],
'invoice_id' => '',
'transection_datetime' => '',
'created_by' => '1',
'status_id' => '1', )
);
dd(UserSubscription::insert($user_subscription_array));
UserSubscription
是我的模特名称。如果成功插入,则返回“ true”,否则返回“ false”。
Laravel解决此问题的另一种方式可能是使用集合并利用时间戳将其循环插入模型。
<?php
use App\Continent;
use Illuminate\Database\Seeder;
class InitialSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
collect([
['name' => 'América'],
['name' => 'África'],
['name' => 'Europa'],
['name' => 'Asia'],
['name' => 'Oceanía'],
])->each(function ($item, $key) {
Continent::forceCreate($item);
});
}
}
编辑:
对不起,我的误会。对于批量插入,这可能会有所帮助,也许这样做可以使您成为好的播种者并对其进行优化。
<?php
use App\Continent;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
class InitialSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$timestamp = Carbon::now();
$password = bcrypt('secret');
$continents = [
[
'name' => 'América'
'password' => $password,
'created_at' => $timestamp,
'updated_at' => $timestamp,
],
[
'name' => 'África'
'password' => $password,
'created_at' => $timestamp,
'updated_at' => $timestamp,
],
[
'name' => 'Europa'
'password' => $password,
'created_at' => $timestamp,
'updated_at' => $timestamp,
],
[
'name' => 'Asia'
'password' => $password,
'created_at' => $timestamp,
'updated_at' => $timestamp,
],
[
'name' => 'Oceanía'
'password' => $password,
'created_at' => $timestamp,
'updated_at' => $timestamp,
],
];
Continent::insert($continents);
}
}
对于类别关系插入,我遇到了相同的问题,并且一无所知,只是在雄辩的模型中,我使用Self()在foreach中具有相同类的实例来记录多个保存和获取ID。
foreach($arCategories as $v)
{
if($v>0){
$obj = new Self(); // this is to have new instance of own
$obj->page_id = $page_id;
$obj->category_id = $v;
$obj->save();
}
}
如果没有“ $ obj = new Self()”,则仅保存一条记录(当$ obj为$ this时)
问题已解决...用于迁移的Alter表
$table->timestamp('created_at')->nullable()->useCurrent();
解:
Schema::create('spider_news', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('source')->nullable();
$table->string('title')->nullable();
$table->string('description')->nullable();
$table->string('daterss')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
has_many
您的模型有关系吗?