drh2so4/modelscope

This is the simple package that allows you to utilize different laravel model scopes dealing with laravel query builders.

dev-master 2020-05-02 07:16 UTC

This package is auto-updated.

Last update: 2024-10-29 06:18:33 UTC


README

N|Solid

This packages provides helpful query builder scopes. This package provide global scopes for every model by making making usage of trait where scopes is defined.

  • Latest Data Retrive
  • Week Data Retrive
  • Year Data Retrive
  • Month Data Retrive
  • Between Date Data Retrive
  • Not Between Date Data Retrive
  • Order Ascending
  • Order Descending
  • Multiple Where Filtering
  • Multiple orWhere Filtering

Installation

Run Composer Require Command

$ composer require drh2so4/modelscope

Use Namespace

use drh2so4\ModelScope\ModelScopes;

then.. Use Trait

use ModelScopes

Overall Structure

<?php

namespace App;

use App\Post;
use drh2so4\ModelScope\ModelScopes;

class Post{
    use ModelScopes;
    //
    }
}

Package Scopes

Packages scopes is made intented to make use various combination of query builder that we daily use of but don't have specified builder yet. This package fulfills that.

Documentation

scopeLatestAll()

    public function scopeGetLatestAll($query){
        return $query->orderBy('updated_at','ASC');
    }

Usage

$data = User::latestAll()->get();

scopeGetLatest($limit)

    public function scopeGetLatestLimit($query,$limit = 5){
        return $query->orderBy('updated_at','ASC')->take($limit);
    }

Usage

$data = User::latestLimit(8)->get();

Retives latest $limit number of data if no parameter given retrives 5(default paremeter value).

scopeTodayData()

      public function scopeTodayData($query){
        $date = Carbon::today();
        $query->whereDate('updated_at',$date);
    }

Usage

$data = User::todayData()->get();

Retives data created today.

scopeWeekData()

public function scopeWeekData($query){
        $date = Carbon::today()->subDays(7);
    $query->whereDate('updated_at','>=',$date);
    }

Usage

$data = User::weekData()->get();

Retives week created data.

scopeWeekDataLimit($limit)

    public function scopeWeekDataLimit($query,$limit = 5){
        $date = Carbon::today()->subDays(7);
    $query->whereDate('updated_at','>=',$date)->take($limit);
    }

Usage

$data = User::weekDataLimit(8)->get();

Retives week $limit number of data if parameter is left empty retives 5(default parameter value) data

scopeMonthData()

    public function scopeMonthData($query){
        $date = Carbon::today()->subMonth();
        $query->whereDate('updated_at','>=',$date);
    }

Usage

$data = User::monthData()->get();

Retives month created data.

scopeMonthDataLimit($limit)

    public function scopeMonthDataLimit($query,$limit = 5){
        $date = Carbon::today()->subMonth();
        $query->whereDate('updated_at','>=',$date)->take($limit);
    }

Usage

$data = User::mothDataLimit(8)->get();

Retives month $limit number of data if parameter is left empty retives 5(default parameter value) data

scopeYearData()

    public function scopeYearData($query){
        $date = Carbon::today()->subYear();
        $query->whereDate('updated_at',$date);
    }

Usage

$data = User::yearData()->get();

Retives month created data.

scopeYearDataLimit($limit)

    public function scopeYearDataLimit($query,$limit = 5){
        $date = Carbon::today()->subYear();
        return $query->whereDate('updated_at','>=',$date)->take($limit);
    }

Usage

$data = User::yearDataLimit(8)->get();

Retives year $limit number of data if parameter is left empty retives 5(default parameter value) data

scopeTodayData()

    public function scopeTodayData($query){
        $date = Carbon::today();
        $query->whereDate('updated_at',$date);
    }

Usage

$data = User::todayData()->get();

Retives data created today.

scopeTillNowFrom($date)

    public function scopeTillNowFrom($query,$date){
        $this->date_validate($date);
        $from = Carbon::createFromFormat('l jS \\of F Y',trim($date));
        return $query->whereDate('updated_at','>=',$from);
    }

Usage

$data = User::tillNowFrom('Thursday 25th of December 1975')->get();

Expects parameter as date in format 'l jS \of F Y'. Retrives all data created from $date till now.

scopeDataBetween($from,$to)

    public function scopeDataBetween($query,$from_date,$to_date){
        $this->dates_validate($from_date,$to_date);
        $from = Carbon::createFromFormat('l jS \\of F Y',trim($from_date));
        $to = Carbon::createFromFormat('l jS \\of F Y',trim($to_date));
        return $query->whereBetween('updated_at',[$from,$to]);
    }

Usage

$data = User::dataBetween('Thursday 25th of December 1975',''Saturday 27th of December 1975')->get();

Expects parameter as date $from and $to in format 'l jS \of F Y'. Retrives all data created $from and $to.

scopeDataNotBetween($from,$to)

    public function scopeDataNotBetween($query,$from_date,$to_date){
        $this->dates_validate($from_date,$to_date);
        $from = Carbon::createFromFormat('l jS \\of F Y',trim($from_date));
        $to = Carbon::createFromFormat('l jS \\of F Y',trim($to_date));
        return $query->whereNotBetween('updated_at',[$from,$to]);
    }

Usage

$data = User::dataNotBetween('Thursday 25th of December 1975',''Saturday 27th of December 1975')->get();

Expects parameter as date $from and $to in format 'l jS \of F Y'. Doesn't retrives data created $from and $to.

scopeWhereFilter($array)

    public function scopeWhereFilter($query, array $filterData = [])
    {
        foreach ($filterData as $key => $value) {
   

            if (is_null($value) || $value === '') continue;

            $scopeName = ucfirst(str::of($key)->camel());

            if (method_exists($this, 'scope' . $scopeName)) {
                $query->$scopeName($value);
            } else if (is_array($value)) {
                $query->whereIn($key, $value);
            } else {
                $query->where($key, $value);
            }
        }
    }

Usage

$data = User::whereFilter(['id' => '2'])->get(); 

Expect associative array where key is column name and value is column value. Retrives all the where condition mention. Above usage equals to User::where('id',2)->get()

Also Supports multiple where condition !

Usage

$data = User::whereFilter(['id' => '2','name' => 'Cat'])->get(); 

Retives data with id = 2 and name = cat

scopeorWhereFilter($array)

    public function scopeorWhereFilter($query, array $filterData = [])
    {
        foreach ($filterData as $key => $value) {
   

            if (is_null($value) || $value === '') continue;

            $scopeName = ucfirst(str::of($key)->camel());

            if (method_exists($this, 'scope' . $scopeName)) {
                $query->$scopeName($value);
            } else if (is_array($value)) {
                $query->orWhere($key, $value);
            } else {
                $query->where($key, $value);
            }
        }
    }

Usage

$data = User::orWhereFilter(['id' => '2'])->get(); 

Expect associative array where key is column name and value is column value. Retrives all the where condition mention. Above usage equals to User::orWhere('id',2)->get()

Also Supports multiple orWhere condition !

Usage

$data = User::orWhereFilter(['id' => '2','name' => 'Cat'])->get(); 

Retives data with id = 2 or name = cat

Todos

Exceptions. More Scopes. Some Testing

License

MIT

DOCTYPE NEPAL ||DR.H2SO4