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?