Awar Pulldozer
Awar Pulldozer

Reputation: 1101

three orwhere and one where laravel 5.3

i have item model and its has search scoop and this is the code

    public static function searchScoop($keyword)
{
    if(!Auth::check())
    {
        $data = Item::limit(30)->where('item_id','like','%'.$keyword.'%')->
        orwhere('item_name','like','%'.$keyword.'%')->
        get(['id','item_id','item_name','item_price','item_total','item_color','item_main_category','item_sub_category','item_sub_sub_category','item_q_in_c','item_last_in','item_total_in','item_inserter','item_state','item_admin','item_area','item_row','item_location','created_at','updated_at','deleted_at']);
    }
    else
    {
        $data = Item::limit(30)->withTrashed()->where('item_id','like','%'.$keyword.'%')->
        orwhere('item_name','like','%'.$keyword.'%')->
        orwhere('item_note','like','%'.$keyword.'%')->
        orwhere('item_barcode','=',$keyword)->
        get(['id','item_id','item_name','item_price','item_total','item_color','item_main_category','item_sub_category','item_sub_sub_category','item_q_in_c','item_last_in','item_total_in','item_inserter','item_state','item_admin','item_area','item_row','item_location','created_at','updated_at','deleted_at']);
    }
    return $data;
}

and im trying to get and statment in the query so i add this

        else
    {
        $data = Item::limit(30)->withTrashed()->
        where('item_area','<>',2)->
        where('item_id','like','%'.$keyword.'%')->
        orwhere('item_name','like','%'.$keyword.'%')->
        orwhere('item_note','like','%'.$keyword.'%')->
        orwhere('item_barcode','=',$keyword)->
        get(['id','item_id','item_name','item_price','item_total','item_color','item_main_category','item_sub_category','item_sub_sub_category','item_q_in_c','item_last_in','item_total_in','item_inserter','item_state','item_admin','item_area','item_row','item_location','created_at','updated_at','deleted_at']);
    }
    return $data;

and it dosnot work i still gat items from area 2 and this is the query when i change the get to toSql

select * from `items` where `item_area` <> ? and `item_id` like ? or `item_name` like ? or `item_note` like ? or `item_barcode` = ? limit 30

any help please

Upvotes: 0

Views: 188

Answers (1)

aaron0207
aaron0207

Reputation: 2333

Try like this:

$data = Item::limit(30)->withTrashed()->
    where('item_area','<>',2) // you need this condition to be true always
    ->where(function ($query) use ($keyword) { // and at least one of this or Am I wrong?
    $query->where('item_id','like','%'.$keyword.'%')->
    orwhere('item_name','like','%'.$keyword.'%')->
    orwhere('item_note','like','%'.$keyword.'%')->
    orwhere('item_barcode','=',$keyword);
    })
    ->get(['id','item_id','item_name','item_price','item_total','item_color','item_main_category','item_sub_category','item_sub_sub_category','item_q_in_c','item_last_in','item_total_in','item_inserter','item_state','item_admin','item_area','item_row','item_location','created_at','updated_at','deleted_at']);

Upvotes: 1

Related Questions