1. Go to this page and download the library: Download dezsidog/laravel-repository library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dezsidog / laravel-repository example snippets
namespace App;
class Post extends Eloquent {
protected $fillable = [
'title',
'author',
...
];
...
}
namespace App\Repository;
use Dezsidog\Repository;
use App\Post;
class PostRepository extends Repository {
protected $model = Post::class;
}
namespace App\Http\Controllers;
use App\Repository\PostRepository;
class PostsController extends BaseController {
/**
* @var PostRepository
*/
protected $posts;
public function __construct(PostRepository $repository){
$this->posts = $repository;
}
}
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(...);
}
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);
}
...
}
namespace App\Repository;
use Dezsidog\Repository;
use App\Post;
class PostRepository extends Repository {
protected $model = Post::class;
protected $expands = [
'user',
'type',
];
}
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.'%')
});
}
}
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);
}
}
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);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.