0

I have the following model with fillable properties:

class Job extends Model {

    protected $fillable = [
      'job_type_id',
      'recruiter_id',
      'start_at',
      'end_at',
      'job_title',
      'job_ref',
      'job_desc'

    ];

    // other stuff
 }

I have a form to update the job which includes some multiselect dropdown lists as follows:

<form class="js-job-posting" enctype="multipart/form-data" accept-charset="UTF-8" action="http://localhost:8000/jobs/2" method="POST">
   <input type="hidden" value="PATCH" name="_method">
   <input type="hidden" value="iElj170OKBLg9THP7iETwsn34iuVhdpBX3hF98UA" name="_token">
   <div class="form-group ">
       <label for="job_title">Job title</label>
       <input id="job_title" class="form-control" type="text" name="job_title">
   </div>

   <div class="form-group ">
      <label for="industry_list">Industry</label>
      <select id="industry_list" class="form-control name="industry_list[]" multiple="" >
         <option value="1">Accounting</option>
         <option value="37">Administration</option>
          ...
       </select>
    </div>
 </form>

In my controller I have the following method to update the job but I get error:

ErrorException in helpers.php line 671: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

It seems like using Auth::user()->recruiter->jobs()->update($request->except('job_type_id')); is causing this error because the request object contains an array for the industry_list. But why should this matter as I've defined the mass fillable attributes on the Job class?

public function update(JobRequest $request, $id)
{
   $job = Job::findOrFail($id);

   Auth::user()->recruiter->jobs()->update($request->except('job_type_id'));

   $job->industries()->sync($request->input('industry_list'));

   flash()->success('Job details have been updated');

   return redirect('/job/' . $id . '/edit');
}

Strangely when I exclude the industry_list as follows: Auth::user()->recruiter->jobs()->update($request->except('job_type_id', industry_list')); and then do an update I get the following sql error:

 SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update `jobs` set `_method` = PATCH, `_token` = iElj170OKBLg9THP7iETwsn34iuVhdpBX3hF98UA,...

Why is the SQL generated including every field in the form for update including _method and _token?

Is it something to do with querybuilder ignoring fillable fields?

adam78
  • 9,668
  • 24
  • 96
  • 207

1 Answers1

0

If you have only one field to update then use:

->update($request->only('job_title'));

otherwise you should exclude all other fields like this:

->update($request->except(['job_type_id', 'industry_list', '_method', '_token']));

$fillable property is not looked while doing an update on a relation.

revo
  • 47,783
  • 14
  • 74
  • 117
  • I already tried that - see my edited post. Doing what you said gives me an SQL error where the SQL update command is trying to update columns _method and _token which clearly the Job model does not contain. It seems like the fillable properties arn't being applied – adam78 Sep 06 '16 at 20:58
  • What is the output of `print_r($request->except(['job_type_id', 'industry_list']));`? – revo Sep 06 '16 at 21:05
  • `Array ( [_method] => PATCH [_token] => iElj170OKBLg9THP7iETwsn34iuVhdpBX3hF98UA [job_title] => test ... )` Simply excluding these fields from the update isn't a proper solution. – adam78 Sep 06 '16 at 21:21
  • If you have only one field to update then use `->update($request->only('job_title'));` otherwise you should exclude all other fields like this `->update($request->except(['job_type_id', 'industry_list', '_method', '_token']));`. `$fillable` is not looked while doing an update on a relation. – revo Sep 06 '16 at 21:25
  • I have lots of fields to update. Is there a way I can do the update on the model and use it's fillable properties. Why does it work on the create method: `Auth::user()->recruiter->jobs()->create($request->all());` – adam78 Sep 06 '16 at 21:30
  • I'm not sure about it and I don't know whether or not it is fixed in Laravel 5.3. But if it is shipped with new Laravel you probably can merge those changes to your installation. – revo Sep 06 '16 at 21:42