ed-fruty / laravel-custom-model-builder by ed-fruty

Custom model query builder for Laravel Eloquent
72
1
2
Package Data
Maintainer Username: ed-fruty
Maintainer Contact: ed.fruty@gmail.com (Eduard)
Package Create Date: 2014-12-02
Package Last Update: 2014-12-03
Language: PHP
License: MIT
Last Refreshed: 2024-12-17 03:01:25
Package Statistics
Total Downloads: 72
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Custom Model Query Builder For Laravel 4

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

  1. Install package via composer
composer require "ed-fruty/laravel-custom-model-builder": "1.0.0"
  1. Add the service provider in app/config/app.php:
'Fruty\LaravelModelBuilder\LaravelModelBuilderServiceProvider',
  1. Publish package configuration
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();