PHP code example of hsntngr / laravel-repository

1. Go to this page and download the library: Download hsntngr/laravel-repository 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/ */

    

hsntngr / laravel-repository example snippets


'providers' => [
    // ...
    Hsntngr\Repository\ServiceProvider::class
];

// ...
use Hsntngr\Repository\IRepositoryManager;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $rm;

    public function __construct(IRepositoryManager $rm)
    {
        $this->rm = $rm;
    }
}
//Now you can easly instantiate a repository with $this->rm->{repositoryKey}

# PostController
public function show($id)
{
    $post = $this->rm->post->findPublishedPost($id);
    
    return view("posts.single",compact("post"));
}

public function show($id)
{
    $post = $this->rm->cache("post:".$id,3*60)
         ->repository("post",function($post) use ($id){
             return $post->findPublishedPost($id);
         });
    // if no minutes defined, it'll cache forever
    return view("posts.single",compact("post"));
}

repository("post")->findPublishedPost($id);

php artisan make:repository User

// app/Repositories/UserRepository.php

// Also you may define custom alias to retrieve repository. By default it uses lower case model name 

php artisan make:repository PostTranslation --key=translate

php artisan repository:list

// post           ->  PostRepository
// user           ->  UserRepository
// comment        ->  CommentRepository