dimanche 28 juin 2015

Updating nested arrays with Laravel

First sorry, i am only developing in php for a month and i do not know how to update this with laravel

I would like to update multiple photos.

Photos have a title description, etc.

My problem is i have no clue how to do it.

I tried the following

public function update(Request $request, Photo $photo)
    {
         // loop throug array of id's
         foreach ($request->photo_id as $id) 
         {
            // find 
             $photos = $photo->find($id);

             // loop throug input fields
             foreach ($request->except('_token', 'tags', 'photo_id') as $key => $value) 
             {
                $photos->$key = $value
                $photos->save();
             }

         }

         die();
    }   

I get the following error

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

So i figured out the problem is with the value

And the results are like this

Key variable

string(5) "title"
string(10) "country_id"
string(7) "city_id"
string(11) "category_id"
string(9) "cruise_id"
string(12) "itinerary_id"
string(4) "desc"
string(6) "people"
string(5) "title"
string(10) "country_id"
string(7) "city_id"
string(11) "category_id"
string(9) "cruise_id"
string(12) "itinerary_id"
string(4) "desc"
string(6) "people"

Value variable results

array(2) {
  [0]=>
  string(9) "title one"
  [1]=>
  string(9) "title two"
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "1"
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "1"
}
array(2) {
  [0]=>
  string(0) ""
  [1]=>
  string(0) ""
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(0) ""
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(0) ""
}

I tried several other attempts but nothing works

Could please someone help me out with this?

Aucun commentaire:

Enregistrer un commentaire