Reputation: 473
A null value is returned, when a multiple select box is empty - this causes an error while using the sync command for a pivot table. How do I make the select box return a blank array '' instead of null?
{!! Form::select('tag_list[]', $tags, '', array('class' => 'select2 form-control', 'multiple')) !!}
The only way I have been able to solve the problem is by checking for null before syncing i.e.
private function syncTags(Company $company, $tags)
{
if (is_null($tags)) {
$tags = [];
}
$company->tags()->sync($tags);
}
Upvotes: 1
Views: 1661
Reputation: 473
So far this is the best solution (still a little ugly!)
$tags = $request->input('tag_list', []);
$company->tags()->sync($tags);
Upvotes: 0
Reputation: 8663
If nothing is selected then Laravel indeed returns null. Manual null check is one way to resovle the case, much like you have already done.
Another option is to choose "something" by default if user has not selected it but this is a ugly hack. I have done it with hidden input which has same name. One will override other and if nothing is selected then hidden input value will be used instead.
Upvotes: 1