Laravel Eloquent Collection Tips

    • Adding a column with constant value to existing collection:
      $existing_col->map(function ($col) {
          $col['url'] = 'http://your.url/here';
          return $col;
      });
    • To add a field to each element of the collection:
      foreach($collection as $c) { $c->{"field"} = '1234';}
    • If update for a column doesn’t work, make sure the column name is added to the fillable array in the Model.  If you do not want to mass update the column, do this instead:
      $collection->col=value; $collection->save();

      No need to add to fillable then.