【问题标题】:laravel mb_strpos() expects parameter 1 to be string, object givenlaravel mb_strpos() 期望参数 1 是字符串,给定对象
【发布时间】:2020-06-01 00:00:46
【问题描述】:

我的搜索功能中有这个语句

if(!empty($project) && !empty($type)){
            $result = Project::where('id', 'LIKE', "%{$project}%")->with(['type', function ($query) use ($type) {
                $query->where('id', 'like', $type);
            }])->get();
}

返回

mb_strpos() 期望参数 1 是字符串,给定对象

逻辑

  1. $project = project.id
  2. $type = type.id
  3. 如果$project and $ type 都有值然后get project where id = $project
  4. 然后急切地将type 加载到该项目其中 id = $type

就像我们说的types, where types.project_id = projects.id and where types.id = $type,但使用急切加载而不是查询。

有什么想法吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您收到此错误是因为 with() 采用关联数组来约束急切加载。试试这个

    if(!empty($project) && !empty($type)){
                $result = Project::where('id', 'LIKE', "%{$project}%")->with(['type'=> function ($query) use ($type) {
                    $query->where('id', 'like', $type);
                }])->get();
    }
    

    【讨论】:

    • 谢谢,这似乎有效。我可以问吗?如果我添加另一个渴望但仍想过滤 type 怎么办?假设->with(['scales', 'categories', 'type' => function(.... 所以我有所有这些急切的模型,但我只在type 上使用我的过滤器?这可能吗?
    • @mafortis 是的,很有可能
    • 知道了 :) 我只需要在 type 关闭 ...}, 'scale'])->get(); 之后添加我的其他渴望
    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2017-08-30
    • 2018-07-30
    • 2018-07-04
    • 2019-05-10
    • 1970-01-01
    • 2020-01-25
    • 2021-02-13
    相关资源
    最近更新 更多