php Laravel whereIn或whereIn

jvidinwx  于 2023-11-16  发布在  PHP
关注(0)|答案(7)|浏览(149)

我正在通过过滤器进行产品搜索:
我的代码:

->where(function($query) use($filter)
{
  if(!empty($filter)){
    foreach ($filter as $key => $value) {           
      $f = explode(",", $value);        
      $query-> whereIn('products.value', $f);
    }           
  }
})

字符串
查询:

and (products.value in (Bomann, PHILIPS) and products.value in (red,white))


但我需要:

and (products.value in (Bomann, PHILIPS) OR products.value in (red,white))


在Laravel中是否有类似的东西:

orWhereIn

q35jwt9p

q35jwt9p1#

你可以在核心中搜索whereIn函数来看到它。在这里。这必须回答你所有的问题

/**
 * Add a "where in" clause to the query.
 *
 * @param  string  $column
 * @param  mixed   $values
 * @param  string  $boolean
 * @param  bool    $not
 * @return \Illuminate\Database\Query\Builder|static
 */
public function whereIn($column, $values, $boolean = 'and', $not = false)
{
    $type = $not ? 'NotIn' : 'In';

    // If the value of the where in clause is actually a Closure, we will assume that
    // the developer is using a full sub-select for this "in" statement, and will
    // execute those Closures, then we can re-construct the entire sub-selects.
    if ($values instanceof Closure)
    {
        return $this->whereInSub($column, $values, $boolean, $not);
    }

    $this->wheres[] = compact('type', 'column', 'values', 'boolean');

    $this->bindings = array_merge($this->bindings, $values);

    return $this;
}

字符串
看,它有第三个布尔参数。祝你好运。

a2mppw5e

a2mppw5e2#

你在Laravel中有一个orWhereIn函数。它接受与whereIn函数相同的参数。
它不在文档中,但你可以在Laravel API中找到它。请参阅Laravel 8或WhereIn文档。
这应该给你这个给予:

$query-> orWhereIn('products.value', $f);

字符串

zfciruhq

zfciruhq3#

$query = DB::table('dms_stakeholder_permissions');
$query->select(DB::raw('group_concat(dms_stakeholder_permissions.fid) as fid'),'dms_stakeholder_permissions.rights');
$query->where('dms_stakeholder_permissions.stakeholder_id','4');
$query->orWhere(function($subquery)  use ($stakeholderId){
            $subquery->where('dms_stakeholder_permissions.stakeholder_id',$stakeholderId);
            $subquery->whereIn('dms_stakeholder_permissions.rights',array('1','2','3'));
    });

 $result = $query->get();

return $result;

字符串
// OUTPUT @input $paddle holderId = 1
//选择group_concat(dms_stakeholder_permissions. config)作为权限,dms_stakeholder_permissionss. rights from dms_stakeholder_permissions,其中dms_stakeholder_permissions. stakeholder_id = 4或(dms_stakeholder_permissions. stakeholder_id = 1 and dms_stakeholder_permissions. rights in(1,2,3))

kognpnkq

kognpnkq4#

是的,orWhereIn是一个可以使用的方法。
我相当肯定它应该给予你想要的结果,但是,如果它没有,你可以简单地使用implode创建一个字符串,然后将其分解(这是对你的数组结构的猜测):

$values = implode(',', array_map(function($value)
{
    return trim($value, ',');
}, $filters));

$query->whereIn('products.value', explode(',' $values));

字符串

1yjd4xko

1yjd4xko5#

是的,orWhereIn where clause方法存在于Laravel中。有关详细信息,请参阅下面的Laravel查询生成器官方文档链接。
文档链接:https://laravel.com/docs/8.x/queries#additional-where-clauses
有两种方法可以得到下面的输出。

and (products.value in (Bomann, PHILIPS) OR products.value in (red,white))

字符串
1.使用orWhereIn

$query = Product::where('color', 'blue')
    ->whereIn('value', ['Bomann', 'PHILIPS'])
    ->orWhereIn('value', ['red', 'white'])
    ->get();


输出量:

select * from `products` where `color` = 'blue' and `value` in (Bomann, PHILIPS) OR `value` in (red,white))


1.使用orWherewhereIn

$query2 = Product::where('color', 'blue')
    ->whereIn('value', ['Bomann', 'PHILIPS'])
    ->orWhere(function ($query) {
        $query->whereIn('value', ['Bomann', 'PHILIPS']);
    })
    ->get();


输出量:

select * from `products` where `color` = 'blue' and `value` in (Bomann, PHILIPS) OR (`value` in (red,white))

rm5edbpk

rm5edbpk6#

例如,如果你有多个whereIn或whereIn条件,你想把括号,这样做:

$getrecord = DiamondMaster::where('is_delete','0')->where('user_id',Auth::user()->id);
if(!empty($request->stone_id))
{
    $postdata = $request->stone_id;
    $certi_id =trim($postdata,",");
    $getrecord = $getrecord->whereIn('id',explode(",", $certi_id))
                           ->orWhereIn('Certi_NO',explode(",", $certi_id));     
}
$getrecord = $getrecord->get();

字符串

vvppvyoh

vvppvyoh7#

**示例1:**不同列名的Laravel模型Eloquent Where查询

public function index()
{
    $data= User::whereIn('name', ['john','dam','smith'])->get();
  
    dd($data);                    
}

字符串

**示例2:**使用Where按类别过滤文章假设您有一个包含category_id列的posts表,并且您希望从特定类别中检索文章。您有一个目标类别ID数组:

$categoryIds = [2, 4, 6];

$posts = Post::whereIn('category_id', $categoryIds)->get();

**示例3:**使用Eloquent按ID检索用户Where Query假设你有一个users表,你想按ID检索用户。你有一个你感兴趣的ID数组:

$userIds = [1, 3, 5, 7];

$users = User::whereIn('id', $userIds)->get();


点击此处查看更多详情。https://woolocker.com/page/how-to-laravel-eloquent-wherein

相关问题