类型错误:传递给BelongsToMany :: formatSyncList()的参数1必须为数组类型,给定为null,在 [英] Type error: Argument 1 passed to BelongsToMany::formatSyncList() must be of the type array, null given, called in

查看:83
本文介绍了类型错误:传递给BelongsToMany :: formatSyncList()的参数1必须为数组类型,给定为null,在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传表单,用户可以添加带有标签的帖子.当我在输入字段中输入标签时,出现此错误

I have upload form which user can add posts with tags. When I enter tag in the input field I've got this error

BelongsToMany.php 866行中的FatalThrowableError:

FatalThrowableError in BelongsToMany.php line 866:

类型错误:传递给Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany :: formatSyncList()的参数1必须为数组类型,给定为null,在

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::formatSyncList() must be of the type array, null given, called in

这就是我在Tag模型中所拥有的

This is what I have in Tag model

public function itemTags()
{
    return $this->belongsToMany('App\Item', 'item_tag');
}

在我的Item模型中

public function taggs()
{
    return $this->belongsToMany('App\Tag', 'item_tag');
}

我认为的字段

<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>

还有控制器

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');
}

错误在此行

    $item->taggs()->sync($request->tags);

这是什么问题?

推荐答案

也许您对标记值$request->tags的请求为空,请尝试像以下方式调用同步:

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);

更新

如果您的请求$request->tags不是类型数组,请尝试以下代码:

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);
}

这篇关于类型错误:传递给BelongsToMany :: formatSyncList()的参数1必须为数组类型,给定为null,在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆