PHP code example of diviky / bright

1. Go to this page and download the library: Download diviky/bright 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/ */

    

diviky / bright example snippets



$filters = [];
// $query->whereRaw('date(created_at) = ?', ['2019-10-12'])
$filters[] = ['date[created_at]' => date('Y-m-d')];

// $query->whereDateBetween('created_at between ? and ? ', ['2019-10-12', '2019-10-22'])
$filters[] = ['range[created_at]' => date('Y-m-d') .' - '. date('Y-m-d')];

// $query->whereBetween('created between ? and ? ', [strtotime('-1 day'), time()])
$filters[] = ['timestamp[created]' => date('Y-m-d') .' - '. date('Y-m-d')];

//
$filters[] = ['unixtime[created]' => date('Y-m-d') .' - '. date('Y-m-d')];
$filters[] = ['between[created]' => date('Y-m-d') .' - '. date('Y-m-d')];

$filters[] = ['filter[name]' => 'bright']; // $query->where('name', '=', 'bright')
$filters[] = ['filter[first_name|last_name]' => 'bright']; // $query->where('first_name', '=', 'bright')->orWhere()
$filters[] = ['lfilter[name]' => 'bright']; // $query->where('name', 'like', '%bright%')
$filters[] = ['rfilter[name]' => 'bright']; // $query->where('name', 'like', 'bright%')
$filters[] = ['efilter[name]' => 'bright']; // $query->where('name', 'like', '%bright')

$rows = DB::table('users')
    ->filter($filters)
    ->get();


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->flattern();
});


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->flat();
});


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->some(['id', 'author.name']);
});


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->except(['author.id']);
});


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->merge(['extra' => 'value']);
});


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->concat(['author.id','author.name']);
});


use App\Models\User;

$rows = Book::with('author')->get();

$rows->transform(function($row) {
    return $row->combine(['author.id', 'author.name']);
});


use App\Models\User;

$books = Book::with('author')->get();

$books = $books->flatterns($except, $exclude);


use App\Models\User;

$books = Book::with('author')->get();

$books = $books->flats($except, $exclude);


use App\Models\User;

$books = Book::with('author')->get();

$books = $books->few(['id', 'author.name']);



// except the relations from merge
$model = $model->flatten($except);

// Take some keys
$model = $model->some(['id']);

// Take except
$model = $model->except(['id']);

// Append keys to attributes
$model = $model->merge(['id' => 1]);

// Apped relation keys to attributes
$model = $model->concat(['relation.id']);

// combination of merge and contact
$model = $model->combine(['relation.id']);

    if ($task == 'sorting') {
        $sorting = $this->input('sorting');
        $this->get('resolver')->getHelper('speed')->sorting('table', $sorting, 'id');

        return [];
    }

Post::whereLike(['name', 'text', 'author.name', 'tags.name'], $searchTerm)->get();


$rows = DB::table('large_table')->iterate(1000);

$rows = DB::table('large_table')->iterate(1000, function($row) {

    return $row;
});



$rows = DB::tables(['roles', 'roles1', 'roles2'])->complexPaginate();



$rows = DB::table('uses')
    ->remember($minutes, $cache_key)
    ->get();

$rows = DB::table('uses')
    ->rememberForever($cache_key)
    ->get();



$rows = DB::table('users')
    ->filter($filters)
    ->deletes();



$rows = DB::table('users')
    ->whereDateBetween('created_at', [date(), date()])
    ->get();



$rows = DB::table('users')
    ->withOutTrashed()
    ->get();



$rows = DB::table('users')
    ->onlyTrashed()
    ->get();



$rows = DB::table('orders')
    ->groupByRaw(['username']);
    ->groupByRaw('price * ? as price_with_tax', [1.0825]);
    ->get()


$rows = DB::table('orders')
    ->selectRaw(['max(price)', 'order_id']);
    ->groupByRaw('price * ? as price_with_tax', [1.0825]);
    ->get()


$rows = DB::table('orders')
    ->selectRaw(['max(price)', 'order_id']);
    ->whereBetweenRaw('max(price)', [1.0825, 2]);
    ->get()


$rows = DB::table('orders')
    ->ordering($data, ['order_id' => 'desc']);
    ->groupByRaw('price * ? as price_with_tax', [1.0825]);
    ->get()

    $result = DB::table('orders')
        ->timestamps()
        ->insert($values)


    $result = DB::table('orders')
        ->timestamps()
        ->update($values)


    $result = DB::table('orders')
        ->timestamps(false)
        ->update($values)



#[View('name', 'layout')] // by default method name has view name
public function index(Request $request)
{
    $data = $request->all();

    $rows = Post::filter($data)
        ->ordering($data, ['ordering' => 'asc'])
        ->paginate();

    return [
        'rows' => $rows,
    ];
}