2017 © Pedro Peláez
 

library laravel-repository

laravel repository

image

dezsidog/laravel-repository

laravel repository

  • Wednesday, May 30, 2018
  • by zedisdog
  • Repository
  • 1 Watchers
  • 2 Stars
  • 121 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 22 % Grown

The README.md

laravel repository

License, (*1)

laravel repositories is used to abstract the data layer, making our application more flexible to maintain., (*2)

bad english, (*3)

will be tested soon., (*4)

language

中文, (*5)

feature

  • yii expands: you can append expands=xxx,xx to url for extra fields by call the relation methods
  • filters: you can append filters[fieldName]=xxx&filters[fieldName2]=xx to url for search records by given condition.
  • custom filters: you can add or cover the filter methods.
  • sort: you can append sorts[name]=asc to url for sort records by given field.
  • custom filters: you can add or cover the sort methods.

install

composer require dezsidog/laravel-repository

usage

create model

namespace App;

class Post extends Eloquent {

    protected $fillable = [
        'title',
        'author',
        ...
     ];

     ...
}

create repository

namespace App\Repository;

use Dezsidog\Repository;
use App\Post;

class PostRepository extends Repository {
    protected $model = Post::class;
}

add repository to controller

namespace App\Http\Controllers;

use App\Repository\PostRepository;

class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $posts;

    public function __construct(PostRepository $repository){
        $this->posts = $repository;
    }
}

then you can

public function someMethod(){
    $this->posts->find(...);
    $this->posts->findBy(...);
    $this->posts->all();
    $this->posts->paginate(...); // deprecated
    $this->posts->create(...);
    $this->posts->update(...);
    $this->posts->delete(...);
}

expands

if model has relations., (*6)

namespace App;

use App\User;
use App\Type;

class Post extends Eloquent {

    protected $fillable = [
        'title',
        'author',
        'user_id',
        'type_id',
        ...
     ];

    public function user(){
        return $this->belongTo(User::class);
    }

    public function type(){
        return $this->belongTo(Type::class);
    }
     ...
}

you can

  • just append expands field to url:
https://www.xxxx.com/path/to/post-list?expands=user,type
  • or make expands auto
namespace App\Repository;

use Dezsidog\Repository;
use App\Post;

class PostRepository extends Repository {
    protected $model = Post::class;
    protected $expands = [
        'user',
        'type',
    ];
}

filters

to search something by, (*7)

https://www.xxxx.com/path/to/post-list?filters[type_id]=3&filters[user_id]=1

you can use relations, (*8)

https://www.xxxx.com/path/to/post-list?filters[type.id]=3&filters[user.id]=1

custom filters

add method named filterBy + key name, to custom filters., (*9)

namespace App\Repository;

use Dezsidog\Repository;
use App\Post;
use Illuminate\Database\Eloquent\Builder;

class PostRepository extends Repository {
    protected $model = Post::class;

    public function filterByNameOrTypeName(Builder $query, $value){
        $query->leftJoin('users', 'users.id', '=', 'posts.user_id')
            ->leftJoin('types', 'type.id', '=', 'posts.type_id')
            ->where(function(Builder $query) use ($value) {
                $query->where('users.username', 'like', '%'.$value.'%')
                    ->orWhere('types.name', 'like', '%'.$value.'%')
            });
    }
}

then make url:, (*10)

https://www.xxxx.com/path/to/post-list?filters[name-or-type-name]=x

you can just cover the exists field

namespace App\Repository;

use Dezsidog\Repository;
use App\Post;
use Illuminate\Database\Eloquent\Builder;

class PostRepository extends Repository {
    protected $model = Post::class;

    public function filterByTitle(Builder $query, $value){
        $query->where('posts.title', '=', $value);
    }
}

sort

to sort something by, (*11)

https://www.xxxx.com/path/to/post-list?sorts[type_id]=asc&sorts[user_id]=desc

you can not use relations, (*12)

custom sorts

add method named sortBy + key name, to custom sorts., (*13)

namespace App\Repository;

use Dezsidog\Repository;
use App\Post;
use Illuminate\Database\Eloquent\Builder;

class PostRepository extends Repository {
    protected $model = Post::class;

    public function sortByName(Builder $query, $value){
        $query->orderby('name',$value);
    }
}

then make url:, (*14)

https://www.xxxx.com/path/to/post-list?filters[name]=asc|desc

you can also cover the exists field, (*15)

The Versions

30/05 2018

dev-develop

dev-develop

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

30/05 2018

dev-master

9999999-dev

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

30/05 2018

4.0.1

4.0.1.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

25/05 2018

4.0.0

4.0.0.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

23/03 2018

3.0.2

3.0.2.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

14/03 2018

3.0.1

3.0.1.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

01/03 2018

3.0.0

3.0.0.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

01/03 2018

2.0.0

2.0.0.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

13/02 2018

1.1.2

1.1.2.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

08/02 2018

1.1.1

1.1.1.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

08/02 2018

1.1.0

1.1.0.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

02/02 2018

1.0.0

1.0.0.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

04/01 2018

0.9.2

0.9.2.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

11/12 2017

0.9.1

0.9.1.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed

18/11 2017

0.9.0

0.9.0.0

laravel repository

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Avatar zed