A package to create repositories in your laravel applications.
saineshmamgain/laravel-repositories is a Laravel package for a package to create repositories in your laravel applications..
It currently has 21 GitHub stars and 49 downloads on Packagist (latest version 1.0.8).
Install it with composer require saineshmamgain/laravel-repositories.
Discover more Laravel packages by saineshmamgain
or browse all Laravel packages to compare alternatives.
Last updated
Laravel Repositories is a Laravel package that makes creating and managing repositories a breeze.
Use the package manager composer to install the package.
composer require saineshmamgain/laravel-repositories
#php artisan make:repository {ModelName}
php artisan make:repository User
This command will create a UserRepository in App\Repositories namespace.
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class UsersController extends Controller{
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required'
'password' => 'required'
]);
$validated = $validator->validated();
UserRepository::init()
->create($validated);
return redirect()->back();
}
}
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
class UsersController extends Controller{
public function update(Request $request, User $user)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required'
'password' => 'required'
]);
$validated = $validator->validated();
UserRepository::init($user)
->update($validated);
return redirect()->back();
}
}
This package also supports softDelete. By default the package will check if the model uses softDelete if yes it will softDelete the model. To delete a model permanently just pass true while calling destroy(true) method.
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
class UsersController extends Controller{
public function destroy(Request $request, User $user)
{
UserRepository::init($user)
->destroy();
return redirect()->back();
}
}
Laravel already provides a nice abstraction for writing queries so querying with using repository can be optional. You can just use models to write queries. But if you still want to use the repository for querying then the repository provides a query() method that proxies model. After calling query() method you can chain all the methods provided by Eloquent.
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
class UsersController extends Controller{
public function index(Request $request)
{
$users = UserRepository::init()
->query()
->where('status', '=', 1)
->paginate();
return view('users.index')->with(compact('users'));
}
}
This package provides some hooks for the create, update, destroy and restore methods.
List of hooks are:
create method beforeCreate and afterCreate.update method beforeUpdate and afterUpdate.destroy method beforeDestroy.restore method afterRestore.beforeSave and afterSave hooks which work on both create and update methods.Usage of Hooks
Let's take a scenario for where you want to hash a users password while creating or updating.
// In App\Repositories\UserRepository create a method beforSave
protected function beforeSave($fields)
{
if(array_key_exists('password', $fields)){
$fields['password'] = Hash::make($fields['password']);
}
return $fields;
}
That's it! Now for every create and update action the password field will be hashed. And yes don't forget to check if password key exists in the $fields array.
$fields is the array of fields that were passed while calling create or update method.
Now let's take another slightly complicated example:
Suppose you are submitting a form while creating a user that also has a field called roles. Since users table doesn't have any roles column then while calling create method Repository will throw an exception.
You can tackle this problem using hooks.
// In App\Repositories\UserRepository create a method beforSave
protected function beforeSave($fields)
{
if(array_key_exists('roles', $fields)){
unset($fields['roles']);
}
return $fields;
}
protected function afterSave($orignal_fields, $fields)
{
if(array_key_exists('roles', $orignal_fields)){
$this->model->roles()->sync($orignal_fields['roles']);
}
return $this->model;
}
All three methods afterSave, afterCreate and afterUpdate will receive two parameters $original_fields, those were submitted originally and $fields, those were returned using before hooks. So you can safely unset all the fields that are not needed while creating/updating a record and use them after the creating/updating the record.
List of Hooks and their Return values
protected function beforeCreate(array $fields)
{
return $fields;
}
protected function afterCreate(array $original_fields, array $fields)
{
return $this->model;
}
protected function beforeUpdate(array $fields)
{
return $fields;
}
protected function afterUpdate(array $original_fields, array $fields)
{
return $this->model;
}
protected function beforeSave(array $fields)
{
return $fields;
}
protected function afterSave(array $original_fields, array $fields)
{
return $this->model;
}
protected function beforeDestroy(bool $isSoftDeletable, bool $permanent)
{
return $this->model;
}
protected function afterRestore()
{
return $this->model;
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to test before submitting a PR.