Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在laravel中插入多行数组_Arrays_Laravel_Eloquent_Tags_Jquery Select2 - Fatal编程技术网

Arrays 如何在laravel中插入多行数组

Arrays 如何在laravel中插入多行数组,arrays,laravel,eloquent,tags,jquery-select2,Arrays,Laravel,Eloquent,Tags,Jquery Select2,嗨,我需要在表中插入一个数组,我不知道怎么做 我使用select2插件选择多个值作为标记,并将它们作为数组插入数据库 输入如下所示 <select name="designation[]" class="form-control" multiple="multiple" id="select2"></select> //script $('#select2').select2({ tags: true, tokenSeparators: [

嗨,我需要在表中插入一个数组,我不知道怎么做 我使用select2插件选择多个值作为标记,并将它们作为数组插入数据库 输入如下所示

<select name="designation[]" class="form-control" multiple="multiple" id="select2"></select>
//script
$('#select2').select2({
        tags: true,
        tokenSeparators: [',', ' '],
        selectOnClose: true
        });
当我提交表单时使用此代码时,我出现了此错误 SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“0”(SQL:插入到
事件中
0
1
)值(222111)) 事件山迁移

public function up()
    {
        Schema::create('event_montants', function (Blueprint $table) {
            $table->increments('id');
            $table->bigInteger('designation');
            $table->unsignedInteger('event_id');
            $table->foreign('event_id')->references('id')->on('events')
            ->onDelete('cascade');
            $table->timestamps();
        });
    }

请帮助我解决此问题

您可以将包含多个关联数组的数组传递到
插入
以插入多条记录。您将需要这些关联数组,其中包含所需字段的键。您可以获取您的输入,这是一个数组,并将其映射到add,以根据需要使用字段作为键获得最终结果:

$data = array_map(function ($var) use ($event) {
    return [
        'designation' => $var,
        'event_id' => $event->id,
    ];
}, $request->input('designation', []));

$event->montant()->insert($data);

尽管您可能只想循环输入数组并调用
create
而不是
insert
,以避免绕过模型并触发时间戳和模型事件。

或者您可以对关系使用save方法

大概是这样的:

$montants = $request->get('designation');

foreach($montants as $value) {
    $montant = new App\Montant(['designation' => $value, 'event_id' => $event_id]);

$event->montant()->save($montant);
}

谢谢你们的帮助,我刚刚用控制器中的代码解决了这个问题

public function store(Request $request){
        $validator = $request->validate([
            'theme' => ['required','unique:events,theme'],
        ]);
        $array_request = $request->get('designation');
        $array = array_chunk($array_request,1);

        $designation = array();
        foreach($array as $k => $v){
            $designation[$k]['designation'] = $v[0];
         }

        $event = Event::create($request->except('designation'));
        $event->montant()->createMany($designation);
        return redirect()->to(route('admin.event.index'))
        ->withFlashSuccess('L\'éventment a bien etait ajouté');
    }

insert
需要一个关联数组,其中键是字段名,您传递的是一个零索引数组,因此它认为您的字段名为0和1。。。不确定要保存的字段是什么,或者为什么要在单个字段中放置数组是的,我知道,但是我如何才能将键名形式0和1更改为“指定”?或者将此数组转换为关联数组,因为当我调试$request->get('designation')时,它会给我以下数组:2[▼ 0=>“222”1=>“111”]对不起,我的英语太差了
public function store(Request $request){
        $validator = $request->validate([
            'theme' => ['required','unique:events,theme'],
        ]);
        $array_request = $request->get('designation');
        $array = array_chunk($array_request,1);

        $designation = array();
        foreach($array as $k => $v){
            $designation[$k]['designation'] = $v[0];
         }

        $event = Event::create($request->except('designation'));
        $event->montant()->createMany($designation);
        return redirect()->to(route('admin.event.index'))
        ->withFlashSuccess('L\'éventment a bien etait ajouté');
    }