fatihirday/eloquent

additional features to eloquent

v0.1.0 2022-02-19 20:58 UTC

This package is auto-updated.

Last update: 2024-04-20 23:52:38 UTC


README

  • ifNull

  • ifCount

  • ifSum

  • sumColumn

  • countColumn

  • concat

  • caseWhen

  • whereLike

  • orWhereLike

  • getSql

  • dumpSql

  • ddSql


Installation

bash command

composer require fatihirday/eloquent

add provider to config/app.php

'providers' => [
    // ...
    Fatihirday\Eloquent\EloquentServiceProvedir::class,
],

Documentation

Prepared for Mysql and PostgreSql

- ifNull

Return the specified value IF the expression is NULL, otherwise return the expression

Model::ifNull('column', 'value', 'responseName');
// or
Model::query()->ifNull('column', 'value', 'responseName');

- ifCount

Count of values satisfying the if condition

Model::ifCount('column', 12, 'responseName');
// or
Model::ifCount('column', '!=', 'value', 'responseName');

- ifSum

Sum of values satisfying the if condition

Model::ifSum('column', '!=', 'value', 'responseName');
// or
Model::ifSum('column', '=', 'value', 'sumColumn', 'responseName');

- sumColumn

Model::sumColumn('column'); // sum(column) as column
// or
Model::sumColumn('column', 'responseName'); // sum(column) as responseName

- countColumn

Model::countColumn('column'); // count(column) as column
// or
Model::countColumn('column', 'responseName'); // count(column) as responseName

- concat

Model::concat(['name', 'id'], 'responseName') // nameid
// or
Model::concat(['name', 'id'], 'responseName', '-') // name-id

- caseWhen

Model::caseWhen([
    'updated_at > created_at' => 'updated_at', // When Then
    'deleted_at > created_at' => 'deleted_at', // When Then
    'created_at', // Else
], 'responseName')

- whereLike

use Fatihirday\Eloquent\Libraries\Enums\Like;

Model::whereLike('columnName', 'value', Like::FIRST); 
// WHERE columnName like '%value'

Model::whereLike('columnName', 'value', Like::MIDDLE); 
// WHERE columnName like '%value%'

Model::whereLike('columnName', 'value', Like::LAST); 
// WHERE columnName like 'value%'

- orWhereLike

use Fatihirday\Eloquent\Libraries\Enums\Like;

Model::where('id', '>', 1)->orWhereLike('columnName', 'value', Like::FIRST); 
// WHERE id > 1 or columnName like '%value'

Model::where('id', '>', 1)->orWhereLike('columnName', 'value', Like::MIDDLE); 
// WHERE id > 1 or columnName like '%value%'

Model::where('id', '>', 1)->orWhereLike('columnName', 'value', Like::LAST); 
// WHERE id > 1 or columnName like 'value%'

- getSql

toSql and getBindings merge

echo Model::where('name', 'value')->getSql();
// select * from `table_name` where `name` = 'fatih'

- dumpSql

dump for getSql

Model::where('name', 'value')->dumpSql();
// select * from `table_name` where `name` = 'fatih'

- ddSql

dd for getSql

Model::where('name', 'value')->ddSql();
// select * from `table_name` where `name` = 'fatih'