三个地方和一个laravel 5.3

时间:2017-09-15 10:44:50

标签: laravel

我有项目模型,它有搜索独家新闻 这是代码

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

我想在查询中获取和判断 所以我添加这个

        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;

它不起作用 我仍然从2区收集物品 这是我将get更改为toSql

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

任何帮助,请

1 个答案:

答案 0 :(得分:1)

试试这样:

$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']);