phplaraveleloquenttwiguserfrosting

Saving a field inside database table using eloquent laravel - Userfrosting


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

Solution

  • 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!