1

This is a sample code inside some controller I have:

$arr = json_decode($post['arr_json']);
for ($i = 0; $i < count($arr); $i++) {
     $port = Port::where('id', $arr[$i]->id)->first();
     $port->company_a_json = $arr[$i];
     $port->save();
}

this is the error im getting:

Call to undefined method Illuminate\\Database\\Eloquent\\Collection::save()

I don't get the thing with the collection. never happened to me before. why is this code, for example not throwing the collection error?

$comps = Comp::where('id', $post['id'])->get();

        foreach ($comps as $comp){
          $comp->base_price_20 = $post['base_price_20'];
          $comp->base_price_40 = $post['base_price_40'];
          $comp->save();
        }
  • 1
    Try to debug your $port variable. print_r($port);... It seems that Laravel Port model is returning Collection instead of model, but it is impossible. I always use that and it works correctly. Or error belongs to another piece of code. – Begmuhammet Kakabayev Feb 11 '17 at 16:38

1 Answers1

2

it finally works! only had to modify this line:

$port->company_a_json = json_encode($arr[$i]);

My guess is until now $arr[$i] returned a Collection to $port->company_a_json. Needed to JSON.stringify it.

many thanks anyway!