Laravel-如何使用单个文本框进行全局搜索



我有一个Laravel-5.8代码,用户可以使用不同的控制字段搜索employee_code、职位和部门。

class HrEmployee extends Model
{
protected $table = 'hr_employees';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'employee_code',
'address',
'email',
'employee_designation_id',
'first_name',
'emp_image',
'last_name',
'other_name',
'department_id',
];
protected $appends = ['full_name'];
public function user()
{
return $this->belongsTo('AppUser');
}
public function designation()
{
return $this->belongsTo('AppModelsHrHrDesignation','employee_designation_id');
}
public function department()
{
return $this->belongsTo('AppModelsHrHrDepartment','department_id');
}   
}

控制器

public function index(Request $request)
{
$userCompany = Auth::user()->company_id;
$render=[];  
$employees = HrEmployee::where('company_id', $userCompany);
$employees=$employees->with('department','designation');
if(isset($request->employee_code))
{
$employees=$employees->where('employee_code','like','%'.$request->employee_code.'%');
$render['employee_code']=$request->employee_code;
}     
if(isset($request->employee_designation_id))
{
$employees=$employees->where('employee_designation_id',$request->employee_designation_id);
$render['employee_designation_id']=$request->employee_designation_id;
}         
if(isset($request->department_id))
{
$employees=$employees->where('department_id',$request->department_id);
$render['department_id']=$request->department_id;
}        
$employees= $employees->paginate(6);
$employees= $employees->appends($render);
$data['employees'] = $employees;  
$data['departments']= HrDepartment::where('company_id', $userCompany)->pluck('dept_name','id');
$data['designations']= HrDesignation::where('company_id', $userCompany)->pluck('designation_name','id');
return view('hr.employees.index',$data);
}

查看

{{ Form::model(request(),['method'=>'get']) }}
<div class="row" style="margin-bottom: 10px">
<div orm-group class="col-sm-3">
{{Form::label('employee_code', 'Employee No.')}}
{{ Form::text('employee_code',null,['class'=>'form-control','placeholder'=>'Employee Code']) }}
</div>
<div class="col-sm-4">
{{Form::label('department_id', 'Department')}}
{{ Form::select('department_id',$departments,null,['class'=>'form-control select2bs4','placeholder'=>'Select Department']) }}
</div>        
<div class="col-sm-4">
{{Form::label('employee_designation_id', 'Designation')}}
{{ Form::select('employee_designation_id',$designations,null,['class'=>'form-control select2bs4','placeholder'=>'Select Designation']) }}
</div>                 
<div class="col-xs-3">
<br>
{{ Form::submit('Search',['class'=>'btn btn-warning']) }}
</div>
{{ Form::close() }}
</div>
<br>
<!-- Default box -->
<div class="card card-solid">
<div class="card-body pb-0">
<div class="row d-flex align-items-stretch">
@if (count($employees))
@foreach($employees as $key => $employee)
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
{{isset($employee->designation) ? $employee->designation->designation_name : ''}}
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Staff ID: {{$employee->employee_code}}</b></h2>
<h2 class="lead"><b>{{$employee->first_name}} {{$employee->last_name}}</b></h2>
<h6 class="lead"><b>Employee Department: </b>{{isset($employee->department) ? $employee->department->dept_name : ''}}</h6>
<h6 class="lead"><b>Employment Date: </b>{{$employee->employment_date ? CarbonCarbon::parse($employee->employment_date)->format('d-m-Y') : 'N/A' }}</h6>
<ul class="ml-4 mb-0 fa-ul text-muted">
<li class="small"><span class="fa-li"><i class="fas fa-lg fa-envelope"></i></span> Email: {{$employee->email}}</li>
<li class="small"><span class="fa-li"><i class="fas fa-lg fa-phone"></i></span> Phone #: {{isset($employee->phone) ? $employee->phone : 'N/A'}}</li>
</ul>
</div>
<div class="col-5 text-center">
@if($employee->emp_image != '')
<img src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="img-circle img-fluid" />
@else
<img class="profile-user-img img-fluid img-circle" src="{{asset('theme/adminlte3/dist/img/default.png')}}" alt="" class="img-circle img-fluid">
@endif
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
@can('employee_show')
<a href="{{ route('hr.employees.show', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Detail
</a>
@endcan
</div>
</div>
</div>
</div>
@endforeach 
@else
<h4 style="text-align:center;">No matching records found</h4>
@endif           
</div>
</div>
<!-- /.card-body -->
<div class="card-footer align-items-center d-flex justify-content-center">
{{ $employees->links() }}
<!-- /.card-footer -->
</div>
<!-- /.card -->
</div>

而不是使用不同的控制字段逐个搜索每个字段。如何使用sing文本框搜索employee_code、first_name、last_name、department和designation?

谢谢

您可以使用以下代码:

$searchText = $request->searchText;
$employees = HrEmployee::with(['department','designation'])->where('company_id', $userCompany);
if($searchText)
{
$employees->where(function ($query) use($searchText) {
$query->where('employee_code','like','%'.$searchText.'%')
->orWhere('first_name','like','%'.$searchText.'%')
->orWhere('last_name','like','%'.$searchText.'%')
->orWhereHas('designation', function($q) use ($searchText) {
$q->where('designation_name', 'like','%'.$searchText.'%'); 
})
->orWhereHas('department', function($q) use ($searchText) {
$q->where('department_name', 'like','%'.$searchText.'%'); 
});
});
}     
$employees = $employees->get();
// For pagination
// $employees = $employees->paginate(6);

此处,designation_name是指定表中的名称字段,department_name

{{ Form::model(request(),['method'=>'get']) }}
<div class="row" style="margin-bottom: 10px">
<div orm-group class="col-sm-3">
{{Form::label('search', 'Search')}}
{{ Form::text('search',null,['class'=>'form-control','placeholder'=>'search']) }}
</div>            
<div class="col-xs-3">
<br>
{{ Form::submit('Search',['class'=>'btn btn-warning']) }}
</div>
{{ Form::close() }}

则控制器函数使用或此处((

public function index(Request $request)
{
$search = $request->search;
$userCompany = Auth::user()->company_id;
$render=[];  
$employees = HrEmployee::where('company_id', $userCompany);
$employees=$employees->with('department','designation');
$employees=$employees->where('employee_code','like','%'.$search.'%')
->orWhere('employee_designation_id',$search)
->orwhere('department_id',$search)
->paginate(6);
$data['employees'] = $employees; 
$data['search'] = $search;

$data['departments']= HrDepartment::where('company_id', $userCompany)->pluck('dept_name','id');
$data['designations']= HrDesignation::where('company_id', $userCompany)->pluck('designation_name','id');
return view('hr.employees.index',$data);
}

最新更新