Laravel eloquent where <> 出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41436599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:02:35  来源:igfitidea点击:

Laravel eloquent where error with <>

phpmysqllaraveleloquent

提问by Muhamad Yulianto

I have query for wherein native php

我有查询,其中在本地的PHP

where type <> 'point'

其中类型 <> '点'

and I try convert to eloquent laravel

我尝试转换为口才 laravel

 ->with('payments',function($query){
      $query->where('type','<>','point');
 })

but it is showing error as follows:

但它显示如下错误:

mb_strpos() expects parameter 1 to be string, object given

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

回答by Alexey Mezenin

You're using wrong syntax. Correct syntax for with()is:

您使用了错误的语法。正确的语法with()是:

->with(['payments' => function ($query) {
    $query->where('type', '<>', 'point');
}])

回答by Mike Harrison

If that is all you need to do with the query then you can just chain it like this:

如果这就是您需要对查询做的所有事情,那么您可以像这样链接它:

->with('payments')->where('type', '<>', 'point') //chain more after this

Correct answer should be thisif you are trying to filter payments where the typeis not equal to point.

如果您尝试过滤不等于 的付款,则正确答案应该是这个typepoint

回答by Manish J

to parse the dynamic parameters within query try this :

要解析查询中的动态参数,请尝试以下操作:

$string = 'points';
->with(['payments' => function ($query) use ($string) {
    $query->where('type', '<>', $string);
}])

this will work!!

这会奏效!!