vendredi 16 août 2019

eloquent update json array to string conversional fails, but using save() works

I am having a problem saving one of my models from a different model. The model in question is my Job model, and the model I am trying to save is my SatusBar model. Within the statusBar model, I have a json database field called fields. In my StatusBar model, I have:

protected $casts = [
  'fields' => 'array'
];

public function status()
{
  return $this->morphTo();
}

In my Jobs controller, where I am trying to update the fields json, I have tried saving it twice:

public function updateStatus(Request $request, Job $job){

$id = $job->status['id'];
$fields = $job->status['fields'];

$id = $job->status['id'];
$fields = $job->status['fields'];
$save = false;

foreach($fields as &$field){
  if($field['field'] == $changeField){
    foreach($field['options'] as $opt){
      if($opt['slug'] == $status){
        $field['selected'] = $status;
        $save = true;
        break 2;
      }
    }
  }
}


if($save){
  //This way works as expected, but doesn't look pretty
  $s = StatusBar::find($id);
  $s->fields = $fields;
  $s->save();

  //This results in an array to string conversion error
  //$job->status()->update(['fields' => $fields]);
}

Why does the second part of the save method result in an array to string conversion error?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KNCELw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire