В laravel вы можете легко сделать это с помощью Query Builder .. как для написанного вами php-кода.
PHP
if (isset($request->search)) {
$WHERE = ' WHERE project_name LIKE "%'.$request->search.'%" OR email LIKE "%'.$request->search.'%"';
} else {
$WHERE = NULL;
}
if(isset($request->offset) && isset($request->limit)){
$sql = DB::select('SELECT id, project_name, email, phone, company, price FROM records "'.$WHERE.'" ORDER BY id LIMIT "'.$request->offset.'","'.$request->limit.'"');
}else{
$sql = DB::select('SELECT id, project_name, email, phone, company, price FROM records ORDER BY id');
}
В Laravel
$qb = DB->table("records")->select("id", "project_name", "phone", "company", "price");
if (isset($request->search)) {
$qb = $qb->where("project_name", "LIKE", "%".$request->search."%")
->orWhere("email", "LIKE", "%". $request->search."%");
}
$result = $qb->orderBy("id", "ASC")->get(); // it would return laravel collection. so just dd() and see the structure.
//....
//....
foreach($result as $row){
dd($row->id); // it will print the id.
}
Здесь вы можете найти руководство о том, как начать работу с построением запроса в laravel https://laravel.com/docs/5.8/queries#selects
ЕСЛИ вы хотите поделиться условиями для несколькихследуйте инструкциям построителя запросов.
$whereData = [];
$orWhereData = [];
if (isset($request->search)) {
$whereData = [
["project_name", "LIKE", "%".$request->search."%"]
];
$orWhereData = [
["email", "LIKE", "%".$request->search."%"]
];
}
$result = DB->table("records")
->select("id", "project_name", "phone", "company", "price")
->where($whereData)
->orWhere($orWhereData)
->orderBy("id", "ASC")
->get(); // it would return laravel collection. so just dd() and see the structure.
//....
//....
foreach($result as $row){
dd($row->id); // it will print the id.
}
// for the gps_locations.
$total = DB::table("gps_locations")
->where($whereData)
->orWhere($orWhereData)
->count();