1354 0 0 0
Last Updated : 2025-04-28 21:19:04
Sometimes, you need to make an Eloquent query that is modified by adding different parts based on other criteria. A typical good example of this is when you submit an advanved search form elements, Then you will need to create a query and then add the criteria based on the different inputs from the form.
$records = new LogvisitModel();
$records = $records->newQuery();
if(count($request->all())>0){
//return \'Search now\';
$dateFrom = $request->input(\'dateFrom\');
$dateTo = $request->input(\'dateTo\');
$userId = $request->input(\'userId\');
//----------------
if ($request->has(\'dateFrom\') && $request->input(\'dateFrom\') != \'\') {
$records->where(\'created_at\',\'>=\',$request->input(\'dateFrom\'));
}
if ($request->has(\'dateTo\') && $request->input(\'dateTo\') != \'\') {
//$records->where(\'created_at\',\'<=\',$request->input(\'dateTo\'));
$theDateTo = Carbon::createFromFormat(\'Y-m-d\', $request->input(\'dateTo\'));
$newDate = $theDateTo->addDays(1);
$newDate = Carbon::createFromFormat(\'Y-m-d H:i:s\', $newDate)->format(\'Y-m-d\');
$records->where(\'created_at\',\'<\',$newDate);
}
if ($request->has(\'userId2\') && $request->input(\'userId2\') != \'\') {
$records->where(\'user_id\',$request->input(\'userId2\'));
}
}else{
}