lundi 2 décembre 2019

how to update data in a database as much as data contained in arrays in Laravel

I want to update the data in the database with the data in my array, I use Foreach to update my data, but I get an error like this

Trying to get property 'id' of non-object", exception: "ErrorException

there is my array

array:35 [
  0 => array:5 [
    "id" => 1
    "post_categori" => "internet"
    "status" => "active"
  ]
  1 => array:5 [
    "id" => 2
    "post_categori" => "banking"
    "status" => "active"
  ]

this is code in my controller

foreach($data as $data)
        {
            $results = Post::where('user_id','=', $data->id)
                        ->first();
            $results->status = $data->status;
            $results->save();
        }

so here I want to change all the status in the database to the status in my array, according to the same user_id (in my database) same with $array-> id



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire