Laravel whereIn OR whereIn

前端 未结 5 1564
盖世英雄少女心
盖世英雄少女心 2020-12-29 21:43

I\'m making a products search by filters:

My code:

->where(function($query) use($filter)
{
  if(!empty($filter)){
    foreach ($filter as $key =&g         


        
相关标签:
5条回答
  • 2020-12-29 22:02

    For example, if you have multiple whereIn OR whereIn conditions and you want to put brackets, do it like this:

    $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();
    
    0 讨论(0)
  • 2020-12-29 22:04

    You could have searched just for whereIn function in the core to see that. Here you are. This must answer all your questions

    /**
     * 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;
    }
    

    Look that it has a third boolean param. Good luck.

    0 讨论(0)
  • 2020-12-29 22:10
    $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 $stakeholderId = 1

    //select group_concat(dms_stakeholder_permissions.fid) as fid, dms_stakeholder_permissionss.rights from dms_stakeholder_permissions where dms_stakeholder_permissions.stakeholder_id = 4 or (dms_stakeholder_permissions.stakeholder_id = 1 and dms_stakeholder_permissions.rights in (1, 2, 3))

    0 讨论(0)
  • 2020-12-29 22:11

    Yes, orWhereIn is a method that you can use.

    I'm fairly sure it should give you the result you're looking for, however, if it doesn't you could simply use implode to create a string and then explode it (this is a guess at your array structure):

    $values = implode(',', array_map(function($value)
    {
        return trim($value, ',');
    }, $filters));
    
    $query->whereIn('products.value', explode(',' $values));
    
    0 讨论(0)
  • 2020-12-29 22:15

    You have a orWhereIn function in Laravel. It takes the same parameters as the whereIn function.

    It's not in the documentation but you can find it in the laravel API. http://laravel.com/api/4.1/

    That should give you this:

    $query-> orWhereIn('products.value', $f);
    
    0 讨论(0)
提交回复
热议问题