aubinlrx/laravelrepositories

Laravel 仓库

0.1.5 2015-05-12 14:12 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:59:57 UTC


README

Laravel 5 的 Eloquent ORM 仓库模式

版本: 0.1.5

可过滤特性

方便过滤查询的方法

用法

<?php

use AubinLrx\LaravelRepositories\Repository;
use AubinLrx\LaravelRepositories\Traits\FilterableTrait;

class BookRepository extends Repository {

   use FilterableTrait;
   
   /**
    * The fields that are filterable
    * @var array
    */
   protected $filterable = ['author_id', 'date_range'];

   public function getAllWithFilter() {
      return $this->applyFilterToQuery( $this->model->query() )->all(); 
   }

   public function filterByDateRange($query, $value) {
        return $query->whereBetween('date', $value);
   }

   public function filterBy($field, $value) {
        $this->addFilter($field, $value);
        return $this;
   }

}

class BookController extends Controller {

    public function __construct(BookRepository $books) 
    {
        $this->books = $books;
    }

    public function index(Request $request) 
    {
        $books = $this->books->filterBy('date_range', $request->only(['str_date', 'end_date']))
           ->filterBy('author_id', $request->only('author_id'))
           ->getAllWithFilter();

        return view('books.index', compact('books'));
    }

}

?>

发布日志

v0.1.5

  • bug 修复:Laravel 5.1 的 create 方法
  • 功能:添加 findOrFail 方法