Skip to content

skycoder026/query-shorter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 

Repository files navigation

Description Build Status

Query Shorter is a laravel package where it shorts the 50% query of your Eloquent query. Sometimes our queries get too long like nested queries, conditional queries so this package creates shorter query for you.

This is a small package to easy and simplify your code.

Installation Process

composer require skycoder/query-shorter

Uses

Model

<?php
namespace App\Models;

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

class Employee extends Model
{
    use  QueryShorter;
}

Controller

Before Using Query Shorter

  $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();

After Using Query Shorter

 $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();

More Packages