PHP code example of kamarkoding / kamarkoding-repository

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

    

kamarkoding / kamarkoding-repository example snippets


'providers' => [
    Kamarkoding\KamarkodingRepository\Providers\RepositoryServiceProvider::class,
];

php artisan make:repository User
config/app.php: 

Created: Class UserRepositoryInterface.php
Created: Class UserRepository.php
Repository created successfully.

app/
└── Repository/
    ├── Contracts/
    │   └── UserRepositoryInterface.php
    └── Eloquent/
        └── UserRepository.php

app/
└── Repository/
    ├── Contracts/
    │   └── <Name>RepositoryInterface.php
    └── Eloquent/
        └── <Name>Repository.php



namespace App\Repository\Contracts;

interface UserRepositoryInterface
{
    //
}



namespace App\Repository\Eloquent;

use App\Repository\Contracts\UserRepositoryInterface;

class UserRepository implements UserRepositoryInterface
{
    //
}
josn


namespace App\Http\Controllers;

use App\Repository\Contracts\UserRepositoryInterface;

class UserController extends Controller
{
    protected $Users;

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

    public function index()
    {
        $data = $this->Users->getAll(); // contoh method
        return view('Users.index', compact('data'));
    }
}
josn


use Livewire\Component;
use App\Repository\Contracts\UserRepositoryInterface;

protected UserRepositoryInterface $userRepository;

class UserIndex extends Component
{
    public UserRepositoryInterface $Users;

    /**
    * Inject repository langsung dari container.
    */
    public function mount(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function delete($id)
    {
        $this->Users->delete($id);
    }

    public function render()
    {
        return view('livewire.user.index');
    }
}