A
A
Andrey Boychenko2016-05-20 22:18:02
MySQL
Andrey Boychenko, 2016-05-20 22:18:02

Inserting data into a database?

Good afternoon! Please advise how to implement the following. There are 2 data arrays, they come from the form, it looks like this.

[ "name" => "csadcasdc"
  "bust" => "90"
  "waist" => "90"
  "hips" => "90"
  "dress" => "22"
  "shoe" => "22"
  "hair" => "Красный"
  "eye" => "Зеленый"
  "stature" => "180"
  "gallery" => array:4 
    0 => "84"
    1 => "85"
    2 => "86"
    3 => "87"
  ]

There is a separate table to store IDs of galleries linked to this entry.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            foreach( $request['gallery'] as $gallery ):
                $modelGallery['gallery'] = $gallery;
                $modelGallery->save();
            endforeach;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //About model
            $model->name = $request['name'];
            $model->about = $request['body'];
            // Body paramethre's
            $model->bust = $request['bust'];
            $model->waist = $request['waist'];//...
            $model->save();

This option adds only the first element of the $request['gallery'] array to the table to store the ID and that's it. The rest of the entries are added as expected. Tell me how to correctly implement the addition of all elements of the $request['gallery'] array? Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-05-20
@Nc_Soft

https://laravel.com/docs/5.2/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question