I have upload form which user can add posts with tags. When I enter tag in the input field I've got this error
FatalThrowableError in BelongsToMany.php line 866:
Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::formatSyncList() must be of the type array, null given, called in
This is what I have in Tag
model
public function itemTags()
{
return $this->belongsToMany('App\Item', 'item_tag');
}
In my Item
model
public function taggs()
{
return $this->belongsToMany('App\Tag', 'item_tag');
}
The field in my view
<div class="form-group">
{!! Form::label('inputTags', 'Tags', array('class'=> 'col-sm-2 control-label')) !!}
{!! Form::text('tags', null, ['class'=>'form-control', 'id'=>'inputTags']) !!}
</div>
And the controller
public function store( ItemRequest $request )
{
$image = $request->file('image');
$filename=null;
if( $image && $image->isValid()){
$extension = $image->getClientOriginalExtension();
$uploadPath = public_path(). '/uploads';
$filename = rand(111,999). '.'. $extension;
$image->move($uploadPath, $filename);
}
$item = new Item;
$item->title = $request['title'];
$item->category_id = $request['category_id'];
$item->description = $request['description'];
$item->user_id = Auth::user()->id;
$item->url = $request['url'];
$item->image = $filename;
if($item->save()){
if(!is_null($filename)) {
$item_image = new Item_Images;
$item_image->image = $filename;
$item_image->item_id = $item->id;
$item_image->published = 1;
$item_image->save();
}
$request->session()->flash('alert-success','Item added successfully.');
}else
$request->session()->flash('alert-error','Can not add item now. Plese tyr again!!.');
$item->taggs()->sync($request->tags);
return redirect()->route('frontend.user.myitems');
}
The error is on this line
$item->taggs()->sync($request->tags);
What is the problem here?