composer require diviky/bright
$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();
filter
method used to filter the database columns in query builder. it accepts requets
object as array
.
Avaliable filters
filter[]
uses the $builder->where($column, $value)
. uses array key as column name and value as value. ex: filter[column]='value'
lfilter[]
uses the $builder->where($column, '%'.$value.'%')
with like match. uses array key as column name and value as value. ex: lfilter[column]='value'
use the |
notation to filter or condition. ex: filter[comments|title]=xxx
use the :
notation to filter with relation table. ex: filter[posts:title]=xxx
use the .
notation to filter the table alias in join query. ex: filter[comments.title]=xxx
use the scope[]
to filter the model scopes. ex: scope[status]=1
will run $builder->status(1)
use parse[]
to DSL Parser for a filter query langague.
Example queries in this language:
price = 100
price != 100
price > 100
price < 100
price <= 100
price >= 100
name =~ "brig%"
price > 100 AND active = 1
status = "pending" OR status = "approved"
product.price > 100 AND category.id = 7
product:price > 100 AND category:id = 7
name =~ "Foo%"
created_at > "2017-01-01" and created_at < "2017-01-31"
status = 1 AND (name = "PHP Rocks" or name = "I ♥ PHP")
Return single model with merged attributes from relations
The flattern($except, $exlcude)
method merge the key and values of releations into primary model attributes and return the combines attributes. Releation keys will overwrite the primary keys if they are same.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->flattern();
});
The flat($except, $exlcude)
method merge the key and values of releations into primary model attributes and return the combines attributes.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->flat();
});
The some($keys)
method get few keys from the relationships and primary model.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->some(['id', 'author.name']);
});
The except($keys)
method get few keys from the relationships and primary model.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->except(['author.id']);
});
The merge($keys)
method add additional key value pairs to model attributes.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->merge(['extra' => 'value']);
});
The concat($keys)
method add relations key values to attributes.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->concat(['author.id','author.name']);
});
The combine($keys)
method to merge and contact the releations and attributes.
use App\Models\User;
$rows = Book::with('author')->get();
$rows->transform(function($row) {
return $row->combine(['author.id', 'author.name']);
});
The flatterns($except, $exlcude)
method merge the key and values of releations into primary model attributes and return the combines attributes. Releation keys will overwrite the primary keys if they are same.
use App\Models\User;
$books = Book::with('author')->get();
$books = $books->flatterns($except, $exclude);
The flats($except, $exlcude)
method merge the key and values of releations into primary model attributes and return the combines attributes.
use App\Models\User;
$books = Book::with('author')->get();
$books = $books->flats($except, $exclude);
The few($keys)
method get few keys from the relationships and primary model.
use App\Models\User;
$books = Book::with('author')->get();
$books = $books->few(['id', 'author.name']);
Return single model with merged attributes from relations
// 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']);
<tbody ajax-content class="table_sortable_body">
...
<td sortable>
<i class="fa fa-arrows-v fa-lg"></i>
<input type="hidden" name="sorting[{{ $row->id }}]" value="{{ $row->ordering }}" />
</td>
</tbody>
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();
If you like fetch all the rows with chunks and modify using callaback
$rows = DB::table('large_table')->iterate(1000);
$rows = DB::table('large_table')->iterate(1000, function($row) {
return $row;
});
If you have data in multiple tables, want to retrive table after table with pagination
$rows = DB::tables(['roles', 'roles1', 'roles2'])->complexPaginate();
If you want to cache the results
$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();
Get non deleted items
$rows = DB::table('users')
->withOutTrashed()
->get();
Get only deleted items
$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()
Set the timestamps 'created_atand
updated_atfor insert and
updated_at` for update
$result = DB::table('orders')
->timestamps()
->insert($values)
$result = DB::table('orders')
->timestamps()
->update($values)
$result = DB::table('orders')
->timestamps(false)
->update($values)
<select name="sent_by" class="form-control" data-select data-select-fetch="{{ url('search/employee') }}" data-fetch-method="post" data-selected="2" label-field="name" value-field="employe_id">
<option value="">Search Employee</option>
</select>
<select name="sent_by" tokenizer>
<option value="">Search Employee</option>
</select>
<select name="sent_by" data-select-ajax="{{ url('search/employee') }}">
<option value="">Search Employee</option>
</select>
<select name="sent_by" data-select-image="{{ url('search/employee') }}">
<option value="">Search Employee</option>
</select>
<select name="countries" data-select-target="#states" data-method="get" data-url="{{ url('search/states/:id') }}">
<option value="">Search Country</option>
</select>
<select name="states" id="states" >
<option value="">Search State</option>
</select>
:id
will be replaced with country id to get states list
Custom attributes used in controllers to configure view, resources and layouts
Avaliable attributes
use Diviky\Bright\Attributes\View;
use Diviky\Bright\Attributes\ViewPaths;
use Diviky\Bright\Attributes\ViewNamespace;
use Diviky\Bright\Attributes\Resource;
use Diviky\Bright\Attributes\ResourceCollection;
#[View('name', 'layout')]
#[View('none')] //No view will be rendered
#[ViewPaths([__DIR__.'views'])]
#[ViewNamespace("package")]
#[Resource('Resources/PostResource', 'post')]
#[ResourceCollection('Resources/PostResource', 'posts')]
#[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,
];
}
The MIT License (MIT). Please see License File for more information.