【问题标题】:Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given,传递给 Illuminate\Database\Query\Builder::cleanBindings() 的参数 1 必须是数组类型,给定字符串,
【发布时间】:2020-11-21 03:33:33
【问题描述】:

我发送了一个数组,但没有工作。我做错了什么?我收到了这个错误。

HTML:

<select name="category[]" class="js-example-basic-single form-control" multiple>
    @foreach ($category as $element)
        <option  value="{{ $element->name }}">
            {{ $element->name }}
        </option>
    @endforeach
</select>

控制器

public function index(Request $request)
{
    if(isset($request['category'])  && $request['category']){
        $search = $request['category'];
        $posts->whereHas('category', function($query) use($search) {
            $query->whereIn('name','like', $search);
        });
    }
}

模特帖

public function category()
{
    return $this->belongsToMany('App\Models\Category','posts_categories','post_id','category_id');
}

【问题讨论】:

标签: php laravel


【解决方案1】:

方法的第二个参数是你的数组。您正在传递一个带有 'like' 的字符串。

https://laravel.com/docs/8.x/queries#additional-where-clauses

/**
* Add a "where in" clause to the query.
*
* @param  string  $column
* @param  mixed  $values
* @param  string  $boolean
* @param  bool  $not
* @return $this
*/
public function whereIn($column, $values, $boolean = 'and', $not = false)

你想要:

 $query->whereIn('name', $search);

已编辑以从 whereIn 中删除 LIKE。

【讨论】:

  • 将'LIKE' 作为第三个参数传递不起作用...$boolean = 'and' 对and 和or 有效,而不是LIKE。 WHERE IN ... 实际上不适用于like。在此处查看答案:stackoverflow.com/questions/34329886/…。他们将不得不使用多个where() 和orWhere() 子句来处理这个问题。
猜你喜欢
  • 2020-10-29
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 2021-03-11
  • 2018-03-22
  • 2018-05-28
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多