PHP code example of joelbutcher / eloquent-repositories

1. Go to this page and download the library: Download joelbutcher/eloquent-repositories 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/ */

    

joelbutcher / eloquent-repositories example snippets


use App\Models\Post;
use JoelButcher\EloquentRepositories\Repository;
use Illuminate\Database\Eloquent\Model;

class PostRepository extends Repository
{
    protected static function model(): string
    {
        return Post::class;
    }

    public function firstForSlug(string $slug): ?Model
    {
        return $this->where('slug', '=', $slug)->first();
    }
}

class UpdatePost
{
    public function __construct(
        private readonly PostValidator $validator,
        private readonly PostRepository $repository
    ) {
    }
    
    public function update(array $data): void
    {
        $this->validator->validate($data);
        
        $this->repository->upsert($data, ['slug'], ['slug']);
    }
}

class UserRepository extends Repository implements UserRepositoryContract
{
    //
} 


class MyServiceProvider extends ServiceProvider
{
    public function boot(): 
    {
        $this->app->bind(UserRepositoryContract::class, UserRepository::::class)
    }
}
shell
php artisan make:repository PostRepository