laravel 在没有表单的laravel 5控制器中手动将数据插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48574905/
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
Insert data into database manually inside a laravel 5 controller without a form
提问by Yulio Aleman Jimenez
I need to insert some data into a sqlite database, manually inside a (Laravel 5) controller and without a form.
我需要将一些数据插入到 sqlite 数据库中,在(Laravel 5)控制器中手动插入并且没有表单。
I've tried with this methods:
我试过这种方法:
1st:
第一:
DB::insert('insert into cars values (?, ?, ?)', [1, "Infinity", "MS500"]);
2nd:
第二:
Car::create([
'brand' => 'Infinity',
'model' => 'MS500',
]);
3rd:
第三:
$car = new Car();
$car->setAttribute('brand', 'Infinity');
$car->setAttribute('model', 'MS500');
$car->save();
The 1stmethod works, but I need to insert thousand of rows, and the time and performance is not so good.
在第一个方法的作品,但我需要插入的行万,时间和性能也不是那么好。
The 2ndand 3rdmethods give me this error:
在第2和第3的方法给我这个错误:
Missing argument 2 for Illuminate\Database\Eloquent\Model::setAttribute(), called in /var/www/html/AppLaravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 525 and defined
I don't know what means this error, I have found a lot about it, and about to populate the data into the database manually, with no results.
我不知道这个错误是什么意思,我已经找到了很多关于它,并且即将手动将数据填充到数据库中,没有结果。
This is the model class:
这是模型类:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
const CREATED_AT = null;
const UPDATED_AT = null;
protected $fillable = ['brand', 'model'];
}
This is the migration class:
这是迁移类:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCarsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('brand');
$table->text('model');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cars');
}
}
EDIT
编辑
I've tested with the bulk insertion too:
我也用批量插入测试过:
4th
第四名
$arr = [
[
'brand' => 'Infinity',
'model' => 'MS500',
],
[
'brand' => 'Toyota',
'model' => 'LK2500',
],
];
DB::insert($arr);
... but I got this new error:
...但我收到了这个新错误:
ErrorException (E_NOTICE) Array to string conversion
回答by Yulio Aleman Jimenez
For an extensive array of arrays with the data (let's say 10000 rows):
对于包含数据的大量数组(假设为 10000 行):
$arr = [
[
'brand' => 'Infinity',
'model' => 'MS500',
],
[
'brand' => 'Toyota',
'model' => 'LK2500',
],
...
// 10000 rows
];
I separated the amount of data to insert in chunks of 500, and I inserted into the database inside of transactions.
我将要插入的数据量分成 500 个块,然后插入到事务内部的数据库中。
$div = 500;
for ($i = 0; $i < intdiv(count($arr), $div); $i++) {
$new_arr = [];
for ($j = ($i * $div); $j < (($i + 1) * $div); $j++) {
$new_arr[] = $arr[$j];
}
DB::transaction(function () use ($new_arr) {
DB::table('hotels')->insert($new_arr);
});
}
I've done this task of this way because SQLite restrict the amount of insertions at the same time. the performance and the speed with this solution is very high.
我以这种方式完成了这项任务,因为 SQLite 同时限制了插入的数量。此解决方案的性能和速度非常高。
Edit
编辑
For all you collection fans:
对于所有收藏爱好者:
collect($arr)->chunk(500)->each(function ($chunk) {
DB::transaction(function () use ($chunk) {
DB::table('hotels')->insert($chunk->toArray());
});
}).
回答by jesuisgenial
What is your PHP version? I think the code you wrote should work.
你的PHP版本是什么?我认为你写的代码应该可以工作。
try with this
试试这个
$car = new Car;
$car->brand = 'Infinity';
$car->model = 'MS500';
$car->save();
=============================
==============================
EDIT: I googled for this and found someone had similar issue. Simply it is a problem with Laravel installation. https://github.com/ollieread/multiauth/issues/29
编辑:我用谷歌搜索这个,发现有人有类似的问题。简单来说就是 Laravel 安装的问题。 https://github.com/ollieread/multiauth/issues/29
回答by Ahmed Shams
$Car= new App\Car;
$options = $Car->options;
$options['brand'] = 'value';
$options['model'] = 'value';
$Car->options = $options;
$Car->save();
it must work
它必须工作