noblemfd's avatar

Laravel Search filters not working as expected

I have this code for search filter:

Model

class AppraisalRespondent extends Model
{

    protected $table = 'appraisal_respondents';

    protected $fillable = [
                  'id',
                  'appraisal_identity_id',
                  'department_id',
                  'employee_id',
                  'is_status',
              ];


    protected $casts = [];

    public function appraisalidentity()
    {
        return $this->belongsTo('App\Models\Appraisal\AppraisalIdentity','appraisal_identity_id','id');
    }       
    public function employee()
    {
        return $this->belongsTo('App\Models\Hr\HrEmployee','employee_id');
    }

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

Controller

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;

public function index(Request $request)
{       
    $userId = Auth::user()->id;
    $render=[];

    $respondents            = AppraisalRespondent::orderBy('created_at', 'DESC')->where('respondent_id', $userId);

    $respondents=$respondents->with('appraisalidentity', 'employee', 'department');
    
        if(isset($request->is_status))
        {
            $respondents=$respondents->where('is_status','like','%'.$request->is_status.'%');
            $render['is_status']=$request->is_status;
        }        
   
        if(isset($request->appraisal_identity_id))
        {
            $respondents=$respondents->where('appraisal_identity_id',$request->appraisal_identity_id);
            $render['appraisal_identity_id']=$request->appraisal_identity_id;
        }      
        
        if(isset($request->appraisal_identity_id))
        {
            $respondents=$respondents->where('department_id',$request->department_id);
            $render['department_id']=$request->department_id;
        }     
        
        if(isset($request->employee_id))
        {
            $respondents=$respondents->where('employee_id',$request->employee_id);
            $render['employee_id']=$request->employee_id;
        }    
        
        $respondents= $respondents->paginate();
        $respondents= $respondents->appends($render);
        $data['respondents'] = $respondents;  
        $data['appraisalidentities']= AppraisalIdentity::where('company_id', $userCompany)->pluck('appraisal_name','id');      
        $data['departments']= HrDepartment::where('company_id', $userCompany)->pluck('dept_name','id'); 
        $data['employees']= HrEmployee::select(
        DB::raw("CONCAT(employee_code,' - ',first_name,' ',last_name) AS name"),'id')
            ->where('company_id', $userCompany)
            ->where('hr_status', 0)
        ->pluck('name', 'id');

    return view('appraisal.appraisal_msfs.index',$data)              
            ->with('respondents', $respondents);    
}

View

{{ Form::model(request(),['method'=>'get']) }}
    <div class="row" style="margin-bottom: 10px">
        <div class="col-sm-2">
            {{ Form::select('appraisal_identity_id',$appraisalidentities,null,['class'=>'form-control select2bs4','placeholder'=>'Select Appraisal Period']) }}
        </div>    
        <div class="col-sm-4">
            {{ Form::select('department_id',$departments,null,['id' => 'department','class'=>'form-control select2bs4','placeholder'=>'Select Department']) }}
        </div> 
        <div class="col-sm-3">
            {{ Form::select('employee_id',$employees,null,['id' => 'employee','class'=>'form-control select2bs4','placeholder'=>'Select Employee']) }}
        </div>   
        <div class="col-sm-2">
            {{ Form::select('is_status',['0'=>'Awaiting Response','1'=>'Treated Successfully'],null,['class'=>'form-control','placeholder'=>'Select Status']) }}
        </div>             

        <div class="col-xs-2">
            <!--<br>-->
            {{ Form::submit('Search',['class'=>'btn btn-warning']) }}

        </div>
        {{ Form::close() }}
    </div>

Javascript

 <script type="text/javascript">
    $('#department').change(function(){
    var departmentID = $(this).val();    
    if(departmentID){
        $.ajax({
           type:"GET",
          url:"{{route('get.employee.departments')}}?department_id="+departmentID,
           success:function(res){               
            if(res){
                $("#employee").empty();
                $("#employee").append('<option>Select Respondent</option>');
                $.each(res,function(key,value){
                    $("#employee").append('<option value="'+key+'">'+value+'</option>');
                });
           
            }else{
               $("#employee").empty();
            }
           }
        });
    }else{
        $("#employee").empty();
    }      
   });       
</script>

There are four dropdowns for filter: appraisal_identity_id, department_id, employee_id and is_status

department_id and employee_id dependent dropdowns.

Out of the four, when I click on search to filter, only is status is working.

appraisal_identity_id is not displaying anything and no error in the console. department_id is diplaying this error in the console:

jquery.min.js:2 GET http://localhost:8888/myapp/get/getEmployeeList?department_id=45 500 (Internal Server Error)

and also display this in the error log:

Class App\Http\Controllers\Appraisal\Request does not exist

How do I make appraisal_identity_id, department_id, employee_id to work in the filter search?

Thanks

0 likes
6 replies
Sergiu17's avatar
Class App\Http\Controllers\Appraisal\Request does not exist

you have to import Request class in your controller

use Illuminate\Http\Request; // <---------- this line 

class AppraisalRespondentController extends Controller
{

    public function index(Request $request)
noblemfd's avatar

@sergiu17 - use Illuminate\Http\Request; is there.

Only is_status is working.

Kindly assist.

Thanks

MichalOravec's avatar

Don't use Laravel Collective. And upgrade your app to latest Laravel version.

noblemfd's avatar

@michaloravec - I am using Laravel-5.8. I've gone far and upgrading the project at this time might be an issue. How do I resolve this?

MichalOravec's avatar

I don't think so, maybe one day and you will have the latest version of Laravel in your project.

Please or to participate in this conversation.