Laravel PHP 过滤器下拉搜索

Laravel PHP filter Drop down Search

需要有关下拉过滤器的帮助。我的任务是分离活跃的、停用的、待批准的等员工。所以我所做的是我创建了一个下拉列表过滤器,以避免为每个状态创建另一个数据 table。请帮我。谢谢!!!

这是控制器

public 函数索引() {

$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);

}

索引页

 @extends('pages.main-content')
@section('css')
@include('layouts.datatables-css')
@endsection
@section('content')
<body class="bg-info">
   <div class="container-fluid">
      <div class="col-1">
         <form action="{{url ('directory') }}" method="GET">
            <div class="row">
               <div class="form-group">
                  <h6>
                     <select id='status' name="status"class="form-control"  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 class="row ">
         <div class="col-12">
            <div class="card-body">
               <table class="table table-hover table-fw-widget " 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 class="Text-center"><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 class="Text-center"><a href="#" id="' . $user->user_id . '" class="text-success mx-1 showIcon" data-bs-toggle="modal" data-bs-target="#showusermodal">
                           <i class="bi bi-eye-fill h5"></i></a>
                           <a href="#" id="' . $user->user_id . '" class="text-danger mx-1 deleteIcon">
                           <i class="bi-trash h5"></i></a>
                        </td>
                     </tr>
                     @endforeach
                  </tbody>
               </table>
            </div>
         </div>
         <div class="col-1"></div>
      </div>
      </form>
   </div>
   {{-- View modal start --}}
   <div class="modal fade" id="showusermodal" tabindex="-1" aria-labelledby="ModalLabel" data-bs-backdrop="static" aria-hidden="true">
      <div class="modal-dialog modal-xl modal-dialog-centered">
         <div class="modal-content">
            <div class="modal-header">
               <h5 class="modal-title" id="Uname">Employee Information</h5>
               <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="#" method="POST" id="showinfo" enctype="multipart/form-data">
               @csrf
               <div class="modal-body p-4 bg-light">
                  <div class="row">
                     <div class="col-sm">
                        <label for="fname">First Name</label>
                        <input type="text" name="fname" id="fname" class="form-control" readonly>
                     </div>
                     <div class="col-sm">
                        <label for="midname">Middle Name</label>
                        <input type="text" name="midname" id="midname" class="form-control" readonly>
                     </div>
                     <div class="col-sm">
                        <label for="lname">Last Name</label>
                        <input type="text" name="lname" id="lname" class="form-control"  readonly>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-lg">
                        <label for="birthdate">Birthdate</label>
                        <input type="text" name="bdate" id="bdate" class="form-control" value="" readonly>
                     </div>
                     <div class="col-lg">
                        <label for="birthdate">Status</label>
                        <input type="text" name="status" id="status" class="form-control" value="" readonly>
                     </div>
                  </div>
               </div>
               <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                  <button type="submit" id="add_employee_btn" class="btn btn-primary">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

您可以编写查询但从末尾删除 get() 函数。

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

然后写这个 if 语句,不要忘记用你的列名自定义我调用 status.name 的列

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

然后路径 $user 到数据表; 你会得到结果

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