php 示例API具有特定列和分页Laravel没有Dingo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 示例API具有特定列和分页Laravel没有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'));
    } 
  
  
}

以上是关于php 示例API具有特定列和分页Laravel没有Dingo的主要内容,如果未能解决你的问题,请参考以下文章

如何从Google表格下载具有特定边距和分页符的PDF文件?

Laravel 获取所有关系和分页关系数据

laravel hydraRaw/fromQuery 和分页急切加载

Laravel Eloquent - hasrsine 公式和分页

如何在laravel 5中的关系列上使用“有”和分页

时间格式的处理和数据填充和分页---laravel