PHP code example of royvoetman / laravel-repository-pattern
1. Go to this page and download the library: Download royvoetman/laravel-repository-pattern 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/ */
royvoetman / laravel-repository-pattern example snippets
$book = Book::find(1);
(new BooksRepository())->delete($book);
class HashPassword
{
public function handle($data, Closure $next): Model
{
if(Arr::has($data, 'password')) {
$data['password'] = bcrypt($data['password']);
}
return $next($data);
}
}
class RemoveBookRelations
{
public function handle(Model $book, Closure $next)
{
$book->author()->delete();
$book->reviews()->delete();
return $next($book);
}
}
class BeforePipe
{
public function handle($data, Closure $next)
{
// Perform actions on the model-data
// e.g. hashing passwords
return $next($data);
}
}
class AfterPipe
{
public function handle($data, Closure $next): Model
{
$model = $next($data);
// Perform actions on Eloquent model
// e.g. saving relationships
return $model;
}
}
class Transaction
{
public function handle($passable, \Closure $next, int $attempts)
{
return DB::transaction(fn () => $next($passable), $attempts);
}
}