nikidze / laravel-repository-generator
Generate Repository from Eloquent models
v0.0.1
2022-03-08 11:41 UTC
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-11-08 17:36:01 UTC
README
Laravel Repositories generator is a package for Laravel 8 which is used to generate reposiotries from eloquent models.
Installation
Run the following command from you terminal:
composer require "nikidze/laravel-repository-generator"
Usage
First, generate your repositories class from eloquent models in Models folder.
php artisan make:repositories
And finally, use the repository in the controller:
<?php namespace App\Http\Controllers; use App\Repositories\PostRepository; class PostController extends Controller { private $post; public function __construct(PostRepository $postRepository) { $this->post = $postRepository; } public function index() { return response()->json($this->post->all()); } }
Available Methods
The following methods are available:
Nikidze\RepositoryGenerator\RepositoryInterface
public function all(); public function trashOnly(); public function find($id); public function findTrash($id); public function findBy($column, $value); public function recent($limit); public function store($data); public function update($data, $id); public function trash($id); public function restore($id); public function destroy($id);
Example usage
Create a new post in repository:
$post = $this->post->store($request->all());
Update existing post:
$post = $this->post->update($request->all(), $post_id);
Delete post:
$post = $this->post->destroy($id);
Get post by post_id:
$post = $this->post->find($id);
you can also chose what relations to eager load:
$post = $this->post->find($id);
Credits
This package is inspired by this great package by @tcharod.