cyberfly
5/23/2017 - 12:25 PM

Example API with specific column and pagination Laravel without Dingo

Example API with specific column and pagination Laravel without Dingo

<?php

class ApplicantController extends Controller
{
    public function index(Request $request)
    {
        $status = array_map('intval',explode(',',$request->input('status')));

        $sortBy = $request->has('sort') ? $request->input('sort') : 'id';
        $limit = $request->has('limit') ? $request->input('limit') : self::$paginationLimit;
        $orderBy = $request->has('order') ? $request->input('order') : 'desc';
        $filterBy = $request->has('filter_by') ? $request->input('filter_by') : 'assign_to';
        $assignedUser = $request->has('owner') ? [$request->input('owner')] : User::whereNotIn('role_id',[2])->get()->pluck('user_id')->toArray();
        $shouldFilter = $request->has('activate_filter') ? $request->input('activate_filter') : 0;
        $filterStatus = $request->has('status') ? $status : ApplicationStatus::all()->pluck('id')->toArray();

        $query = $this->applicant->whereHas('application', function($q) use ($filterStatus) {
            //$submitted_status = ApplicationStatus::all()->pluck('id')->toArray();

            $q->whereIn('application_status_id',$filterStatus);

        })->with('application','application.previousApplicationStatus','application.applicationStatus')

            ->with(['assigner' => function($q){

            $q->select('user_id','email','role_id');

        }])->with(['parent' => function($query){

            $query->select('id','first_name','middle_name','last_name');
        }])->with( ['application.attachment' => function ($q) {
            $q->where('code', '=', 'minute_meeting');
        }])->with( ['travel_document' => function ($q) {
            $q->select('applicant_id','travel_document_number');
        }])



            ->newQuery();

        if($shouldFilter == 1 ) {
            $query->whereIn($filterBy,$assignedUser);
        }

        //search by textfield (first_name/last_name/email/application_no/travel_document_number)

        if (!empty($request->filter_keyword)) {

            $filter_keyword = $request->filter_keyword;
            
            $query->where(function ($query) use ($filter_keyword) {
                            $query->orWhere('first_name', 'like' , '%'.$filter_keyword.'%')
                                ->orWhere('last_name', 'like' , '%'.$filter_keyword.'%')
                                ->orWhere('email', $filter_keyword)
                                ->orWhereHas('application', function ($query) use ($filter_keyword) {
                                    $query->where('application_no', $filter_keyword);
                                })
                                ->orWhereHas('travel_document', function ($query) use ($filter_keyword) {
                                    $query->where('travel_document_number', $filter_keyword);
                                });
                        });
        
        }

        $query->orderBy($sortBy,$orderBy);

        $applicant = $query->paginate($limit);
        $applicant->appends($request->all());

        return response()->json(compact('applicant'));
    } 
  
  
}