PHP code example of skycoder / query-shorter

1. Go to this page and download the library: Download skycoder/query-shorter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

skycoder / query-shorter example snippets



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Skycoder\QueryShorter\QueryShorter;

class Employee extends Model
{
    use  QueryShorter;
}

  $employees = Employee::query()
                ->when($request->filled('name'), function($q) use($request) {
                    $q->where('name', 'like', '%' . $request->name . '%');
                })
                ->when($request->filled('department_id'), function($q) use($request) {
                    $q->where('name', $request->department_id);
                })
                ->when($request->filled('designation_id'), function($q) use($request) {
                    $q->where('designation_id', '!=', $request->designation_id);
                })
                ->when($request->filled('age'), function($q) use($request) {
                    $q->where('age', '<', $request->age);
                })
                ->when($request->filled('from_date'), function($q) use($request) {
                    $q->where('joining_date', '>=', $request->from_date);
                })
                ->when($request->filled('to_date'), function($q) use($request) {
                    $q->where('joining_date', '<=', $request->to_date);
                })
                ->when($request->filled('from_retirement_date'), function($q) use($request) {
                    $q->where('retirement_date', '>=', $request->from_retirement_date);
                })
                ->when($request->filled('to_retirement_date'), function($q) use($request) {
                    $q->where('retirement_date', '<=', $request->to_retirement_date);
                })
                ->when(request()->filled('from'), function($qr) use($request) {
                   $qr->where('date', '>=', $request->from);
                })
                ->when(request()->filled('to'), function($qr) use($request) {
                   $qr->where('date', '<=', $request->to);
                })
                ->when($request->filled('division'), function($q) use($request) {
                    $q->whereHas('contact_info', function($qr) use($request) {
                        $qr->where('division', $request->division);
                    });
                })
                ->withCount(['employee_type as employee_type_name' => function($q) { 
                    $q->select(DB::raw('name')); 
                }])
                ->latest()
                ->get();

 $employees = Employee::query()
                        ->likeSearch('name')
                        ->searchByField('department_id') // check if the request has `department_id` value then we query
                        ->searchByField('designation_id', "!=")
                        ->searchByField('age', "<")
                        ->searchDateFrom('joining_date') // `joining_date` is database field and `from_date` from request
                        ->searchDateTo('joining_date') // `joining_date` is database field and `to_date` from request
                        ->searchDateFrom('retirement_date', 'from_retirement_date') // `retirement_date` is database field and `from_retirement_date` from request
                        ->searchDateTo('retirement_date', 'to_retirement_date') // `retirement_date` is database field and `to_retirement_date` from request
                        ->dateFilter()
                        ->searchFromRelation('contact_info', 'division')
                        ->selectName('employee_type')
                        ->selectName(['department', 'designation', 'grade'])
                        ->latest()
                        ->get();