PHP code example of akunbeben / laravository

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

    

akunbeben / laravository example snippets


'providers' => [
  ...
  App\Providers\RepositoryServiceProvider::class,
],


namespace App\Repositories\Eloquent;

use Akunbeben\Laravository\Repositories\Eloquent\BaseRepository;

use App\Repositories\Interfaces\UserRepositoryInterface;
use App\Models\User;

class UserRepository extends BaseRepository implements UserRepositoryInterface
{
  protected $model;

  /**
   * Your model to use the Eloquent
   * 
   * @param User $model
   */
  public function __construct(User $model)
  {
    $this->model = $model;
  }
}

namespace App\Repositories\Interfaces;

interface UserRepositoryInterface
{
    
}

...
class RepositoryServiceProvider extends ServiceProvider
{
  ...
  public function register()
  {
    ...
    $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
  }
}

namespace App\Http\Controllers;

use App\Repositories\Interfaces\UserRepositoryInterface;

class UserController extends Controller
{
  protected $userRepository;

  public function __construct(UserRepositoryInterface $userRepository) {
    $this->userRepository = $userRepository;
  }
}


class UserController extends Controller
{
  protected $userRepository;

  public function __construct(UserRepositoryInterface $userRepository) {
    $this->userRepository = $userRepository;
  }

  ...

  public function store(SomeFormRequest $request)
  {
    return $this->userRepository->create($request->validated());
  }
}

php artisan repository:provider

php artisan make:repository User