2017-09-15 44 views
0

我有產品型號和它的擁有搜索瓢 ,這是在查詢 代碼3 orwhere和一個地方laravel 5.3

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

和IM試圖讓和statment,所以我加入這個

 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; 

而且dosnot工作 我還是從區域2 騎上項目,這是查詢時我改變得到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

嘗試'這裏( '!=' 'item_area',2)' –

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

我這樣做,並gat內部服務器錯誤 –

+1

已修復,請立即嘗試! – aaron0207

+0

其工作感謝之人 –