lab2view/laravel-repository-generator
Laravel Repositories generator
Laravel Repositories Generator is a package for Laravel used to generate repositories from Eloquent models.
Installation
Run the following command from your terminal:
composer require "lab2view/laravel-repository-generator"
Usage
Generate repository classes from Eloquent models in the Models folder:
php artisan make:repositories
Use the generated repository in the controller:
<?php namespace App\Http\Controllers;
use App\Repositories\PostRepository;
class PostController extends Controller {
private $postRepository;
public function __construct(PostRepository $postRepository)
{
$this->postRepository = $postRepository;
}
public function index() {
return response()->json($this->postRepository->getAll());
}
}
Available Methods
The following methods are available:
Lab2view\RepositoryGenerator\RepositoryInterface
public function exists(string $key, $value, $withTrashed = false)
public function getByAttribute(string $attr_name, $attr_value, $relations = [], $withTrashed = false, $selects = [])
public function getPaginate(int $n, $relations = [], $withTrashed = false, $selects = []);
public function store(Array $inputs)
public function getById($id, $relations = [], $withTrashed = false, $selects = [])
public function search($key, $value, $relations = [], $withTrashed = false, $selects = [])
public function getAll($relations = [], $withTrashed = false, $selects = [])
public function countAll($withTrashed = false)
public function getAllSelectable($key)
public function update($id, Array $inputs)
public function destroy($id)
public function destroyAll()
public function forceDelete($id)
public function restore($id)
Example usage
Create a new post in repository:
$post = $this->postRepository->store($request->all());
Update an existing post:
$post = $this->postRepository->update($post_id, $request->all());
Delete post:
$post = $this->postRepository->destroy($id);
Get a post by post_id:
$post = $this->postRepository->getById($id);
you can also choose what relations to eager load:
$post = $this->postRepository->getById($id, ['comments']);
Contributing
Thank you for considering contributing to this Laravel package.
Credits
This package is inspired by this great package by @bosnadev.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 1.9.1 | ^7.4| | ^6.0| | MIT | |
| 1.9 | ^7.4| | ^6.0| | MIT | |
| 1.8 | ^7.4| | ^6.0| | MIT | |
| 1.7 | ^7.4| | ^6.0| | MIT | |
| 1.6 | ^7.4| | ^6.0| | MIT | |
| 1.5 | ^7.4| | ^6.0| | MIT | |
| 1.4 | ^7.0| | ^6.0| | MIT | |
| 1.3 | ^7.0 | ^6.0| | MIT | |
| 1.2 | ^7.0 | ^5.4| | MIT | |
| 1.1 | ^7.0 | ^5.4| | MIT | |
| 1.0 | ^7.0 | ^5.4| | MIT |