0

hi i need to insert an array in a table and i don't know how to do i'm using the select2 plugin to select multiple values as tags and insert them in database as an array the input looks like this

<select name="designation[]" class="form-control" multiple="multiple" id="select2"></select>
//script
$('#select2').select2({
        tags: true,
        tokenSeparators: [',', ' '],
        selectOnClose: true
        });

EventController

public function store(Request $request){
        $validator = $request->validate([
            'theme' => ['required','unique:events,theme'],
        ]);
        $event = Event::create($request->except('designation'));
        $event->montant()->insert($request->get('designation'));


        return redirect()->to(route('admin.event.index'))
        ->withFlashSuccess('L\'éventment a bien etait ajouté');
    }

with this code when i submit the form i have this error SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into event_montants (0, 1) values (222, 111)) event_montant migration

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();
        });
    }

please help i need to resolve this

2
  • 1
    insert is expecting an associative array where the keys are the field names, you are passing a zero-indexed array so it thinks your fields are named 0 and 1 ... not sure what field you are trying to save or why you would be trying to put an array in a single field Commented Dec 15, 2019 at 22:09
  • yeah i know but how can i change the key name form 0 and 1 to 'designation'? or convert this array to an associative array , cause when i debug $request->get('designation') it gives me this array:2 [▼ 0 => "222" 1 => "111" ] and sorry for my english Commented Dec 15, 2019 at 22:15

3 Answers 3

1

You can pass an array containing numerous associative arrays to insert to insert multiple records. You will need to have these associative arrays with keys for the field you want. You could get your input, which is an array and map it to add get a final result with the field as the key as needed:

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

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

Though you may just want to loop through your array of inputs and call create instead of insert to not bypass the model and get your timestamps and model events fired.

Sign up to request clarification or add additional context in comments.

Comments

0

Or you could use the save method on the relationship.

Something like this:

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

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

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

Comments

0

thank you guys for the help i just solved the problem with this code in the controller

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é');
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.