noblemfd
3 months ago
278
5
Laravel

How to Implement Search in Controller

Posted 3 months ago by noblemfd

I have this model class:

class HrEmployee extends Model
{
  protected $table = 'hr_employees';

  protected $fillable = [
              'employee_code',
              'address',
              'company_id',
              'email',
              'employment_date',
              'first_name',
              'last_name',
              'local_government_id',
              'nationality_id',
              'other_name',
              'password',
              'phone',
              'resignation_date',
              'is_hod',
              'department_id',
          ];

protected $dates = [
    'created_at',
    'updated_at',
    'date_of_birth',
    'employment_date',
    'resignation_date',
];

public function company()
{
    return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}

public function department()
{
    return $this->belongsTo('App\Models\Organization\OrgDepartment','department_id');
}

public function fullName()
{
    return $this->first_name . ' ' . $this->other_name . ' ' . $this->last_name;
}  
}

Controller

class HrEmployeesController extends Controller
{
 public function index()
 {
    $userCompany = Auth::user()->company_id;

    if (Auth::user()->hasRole('Super Admin')) {
        $employees = HrEmployee::paginate(6);
    } else {
        $employees = HrEmployee::where('company_id', $userCompany)->paginate(6);
    }
    return view('hr.employees.index')->with('employees', $employees);
 }
}

view

        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>

                    <th width="10">
                        #
                    </th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Employee Code </th>
                    <th>Designation</th>
                    <th>Department</th>
                    <th>Employment Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @foreach($employees as $key => $employee)
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{$employee->first_name}} {{$employee->last_name}}
                        </td>
                        <td>
                            {{$employee->email}}
                        </td>
                        <td>
                            {{isset($employee->employee_code) ? $employee->employee_code : ''}}
                        </td>
                        <td>
                            {{isset($employee->designation) ? $employee->designation->designation_name : ''}}
                        </td>
                        <td>
                            {{isset($employee->department) ? $employee->department->dept_name : ''}}
                        </td>
                        <td>
                            {{$employee->employment_date ? Carbon\Carbon::parse($employee->employment_date)->format('d-m-Y') : 'N/A' }}

                                           
                </tr>
                @endforeach 
            </tbody>
        </table>

I want the users to be able to search by Name (first_name and last_name) - textbox, department - dropdownlist and employee_code - textbox. When the user is done, he clicks on submit button, then the result is displayed.

How do I implement this from both controller and view.

Thank you.

Please sign in or create an account to participate in this conversation.