Home > Software engineering >  Laravel PHP filter Drop down Search
Laravel PHP filter Drop down Search

Time:05-23

need help with the dropdown filter. my task is to separate the active, deactivate, for approval, etc. employees. so I did was I created a dropdown list filter to avoid creating another data table for every status. please help me. thank you!!!

this the controller

public function index() {

$user = DB::table("user_basic")
->leftjoin("user_status","user_status.status_id" , "=" , "user_basic.user_id")       
->select("user_status.*", "user_basic.*")
->get();
$datatables = datatables::of($user);



return view("pages.Directory.index")->with("user",$user);

}

the index page

 @extends('pages.main-content')
@section('css')
@include('layouts.datatables-css')
@endsection
@section('content')
<body >
   <div >
      <div >
         <form action="{{url ('directory') }}" method="GET">
            <div >
               <div >
                  <h6>
                     <select id='status' name="status"  style="width: 200px">
                        <option value = "ACTIVE">Active</option>
                        <option value = "APPROVED">Approved</option>
                        <option value = "DEACTIVATED">Deactivated</option>
                        <option value = "DENIED"> Denied</option>
                        <option value = "DISAPPROVED">Disapproved</option>
                        <option value = "FOR APPROVAL">For Approval</option>
                        <option value = "INCOMPLETE">Incomplete</option>
                        <option value = "STARTUP">Startup</option>
                     </select>
                     <div class = "column-md-4">
                  </h6>
                  <button type ="submit" class ="btn btn-light">Filter <i class ="bi bi-funnel"></i></button>
                  </div>
               </div>
            </div>
         </form>
      </div>
   </div>
      <div >
         <div >
            <div >
               <table  id="table4">
                  <thead>
                     <td>ID</td>
                     <td><b>Username</b></td>
                     <td><b>Name</b></td>
                     <td><b>Birthdate</b></td>
                     <td><b>Status</b></td>
                     <td ><b>---------</b></td>
                     </tr>
                  </thead>
                  <tbody>
                     @foreach ($user as $user)
                     <tr>
                        <td>{{$user->user_id}}</td>
                        <td>{{$user->user_xusern}}</td>
                        <td>
                           <?php
                              $arr = array($user->user_xfirstname,
                                    $user->user_xmiddlename,
                                    $user->user_xlastname);
                                    echo join(" ",$arr);
                                    ?>
                        </td>
                        <td>{{$user->user_xbirthdate}}</td>
                        <td>{{$user->status_xtitle}}</td>
                        <td ><a href="#" id="' . $user->user_id . '"  data-bs-toggle="modal" data-bs-target="#showusermodal">
                           <i ></i></a>
                           <a href="#" id="' . $user->user_id . '" >
                           <i ></i></a>
                        </td>
                     </tr>
                     @endforeach
                  </tbody>
               </table>
            </div>
         </div>
         <div ></div>
      </div>
      </form>
   </div>
   {{-- View modal start --}}
   <div  id="showusermodal" tabindex="-1" aria-labelledby="ModalLabel" data-bs-backdrop="static" aria-hidden="true">
      <div >
         <div >
            <div >
               <h5  id="Uname">Employee Information</h5>
               <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="#" method="POST" id="showinfo" enctype="multipart/form-data">
               @csrf
               <div >
                  <div >
                     <div >
                        <label for="fname">First Name</label>
                        <input type="text" name="fname" id="fname"  readonly>
                     </div>
                     <div >
                        <label for="midname">Middle Name</label>
                        <input type="text" name="midname" id="midname"  readonly>
                     </div>
                     <div >
                        <label for="lname">Last Name</label>
                        <input type="text" name="lname" id="lname"   readonly>
                     </div>
                  </div>
                  <div >
                     <div >
                        <label for="birthdate">Birthdate</label>
                        <input type="text" name="bdate" id="bdate"  value="" readonly>
                     </div>
                     <div >
                        <label for="birthdate">Status</label>
                        <input type="text" name="status" id="status"  value="" readonly>
                     </div>
                  </div>
               </div>
               <div >
                  <button type="button"  data-bs-dismiss="modal">Close</button>
                  <button type="submit" id="add_employee_btn" >Add Employee</button>
               </div>
            </form>
         </div>
      </div>
   </div>
   {{-- view modal end --}}
   <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
   <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</body>
@endsection
@section('scripts')
@include('layouts.datatables-scripts')
<script type="text/javascript">
    $(document).ready(function()
   { 
     $('#table4').DataTable();
   });
</script>
@endsection




enter code here

CodePudding user response:

you can write your query but remove get() function from the end .

   $user = DB::table("user_basic")
    ->leftjoin("user_status","user_status.status_id" , "=" , "user_basic.user_id")       
    ->select("user_status.*", "user_basic.*");

then write this if statement and don't forget to customize the column where i called status.name with your column name

  if (request()->filled('status') && request('status') !== null) {
        $data = $data->where('status.name', request()->status);
    }

then path $user to datatables; you will get the result

$datatables = datatables::of($user);
  • Related