Skip to main content

How to create search filter with ajax in Laravel 8?

Create search filter with ajax in Laravel 8


<?php
function ajax_group_list(Request $request){
$group = DB::table('group');
$group->join('users', 'users.id', '=', 'group.userid_createdby');
if (!empty($request->search)) {
$group->where('group.gg_name', 'Like', '%' . $request->search . '%');
$group->orWhere('users.displayname', 'Like', '%' . $request->search . '%');
$group->orWhere('users.email', 'Like', '%' . $request->search . '%');
}

if (!empty($request->search_city)) {
$group->where('group.gg_city', 'Like', '%' . $request->search_city . '%');
}

if (!empty($request->search_state)) {
$group->where('group.gg_state', 'Like', '%' . $request->search_state . '%');
}

if (!empty($request->search_country)) {
$group->where('group.gg_country', 'Like', '%' . $request->search_country . '%');

if(!empty($request->date_range)){
$string = explode(' - ',$request->date_range);
$date1 = explode('/',$string[0]);
$date2 = explode('/',$string[1]);
$search_fromdate1 = $date1[0].'-'.$date1[1].'-'.$date1[2];
$search_todate1 = $date2[0].'-'.$date2[1].'-'.$date2[2];
$search_fromdate = date("Y-m-d", strtotime($search_fromdate1));
$search_todate = date("Y-m-d", strtotime($search_todate1)); 
$group->whereDate('group.created_at', '>=', $search_fromdate);
$group->whereDate('group.created_at', '<=', $search_todate);
}
$group->where('group.gg_type', 0);
$group->groupBy('group.id');
$group->select('group.*','users.displayname','users.email','users.profile_pic');
$group = $group->get();
$group_rows = view('admin.group.group-row', ['group' => $group])->render();
return response()->json(array('success' => 1, 'list_row'=>$group_rows));
}

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...