Type error: Argument 1 passed to BelongsToMany::fo

2019-08-26 21:04发布

问题:

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?

回答1:

Maybe your request of tag value $request->tags get empty, Try to call sync like:

$syncTagData = array();
//Passing empty array if tag request is empty...
if(!empty($request->tags)){
    $syncTagData= $request->tags;
}

$item->taggs()->sync($syncTagData);

Update

If your request $request->tags not the type array, try below code:

$syncTagData = array();
//Passing empty array if tag request is empty...
if(!empty($request->tags)){
  array_push($syncTagData, $request->tags);
}


回答2:

Today I got same error in many to many relationship between a plot and features. Reason behind this was user wasn't selecting the features and null was passing to sync()

$plot->featureset()->sync($request->features);

Solution:

$feature_set = ($request->features) != null) ? $request->features : [];

$plot->featureset()->sync($feature_set);

Regards



回答3:

Try this it works for my project.

    if (isset($request->tags)) {

        $intern->tags()->sync($request->input('tags'), false);

    } else {

        $intern->tags()->sync(array());

    }