Downloads
Stars
Version
Sometimes we need to extends basic query builder. Of course scopes decide many our problems, but they can't return value. For example we need to use caching query, but we must calculate/read from config/db caching time. Write it in every cahing query is not good choise. So we can define custom query builder. So, do it.
#Installation
composer require "ed-fruty/laravel-custom-model-builder": "1.0.0"
app/config/app.php
:'Fruty\LaravelModelBuilder\LaravelModelBuilderServiceProvider',
php artisan config:publish ed-fruty/custom-model-builder
#Usage
Now, create file app/ExampleBuilder.php
and put here:
<?php
class ExampleBuilder extends Illuminate\Database\Eloquent\Builder
{
/**
* Add new method to builder
*
* @access public
*/
public function cacheIt()
{
$cacheTime = Config::get('someting.where.cache.time.saved');
return $this->getModel()
->remember($cacheTime)
->first();
}
}
Now register out ExampleBuilder, edit confiiguration file app/config/ed-fruty/custom-model-builder/main.php
, set builderClass
to ExampleBuilder
Create any test model (or use existing)
class MyModel extends Eloquent
{
// redeclare builder by using trait
use Fruty\LaravelCustomBuilder\CustomBuilderTrait;
}
Now we can use it.
$result = MyModel::where('id', 5)->cacheIt();