PHP code example of salmanzafar / laravel-repository-pattern

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

    

salmanzafar / laravel-repository-pattern example snippets


php artisan make:repo Car



namespace App\Repositories;

interface CarRepositoryInterface
{
    /**
     * Get's a record by it's ID
     *
     * @param int
     */
    public function get($id);

    /**
     * Get's all records.
     *
     * @return mixed
     */
    public function all();

    /**
     * Deletes a record.
     *
     * @param int
     */
    public function delete($id);

    /**
     * Updates a record.
     *
     * @param int
     * @param array
     */
    public function update($id, array $data);
}



namespace App\Repositories;

use App\Car;


class CarRepository implements CarRepositoryInterface
{
    /**
     * Get's a record by it's ID
     *
     * @param int
     * @return collection
     */
    public function get($id)
    {
        return Car::find($id);
    }

    /**
     * Get's all records.
     *
     * @return mixed
     */
    public function all()
    {
        return Car::all();
    }

    /**
     * Deletes a record.
     *
     * @param int
     */
    public function delete($id)
    {
        Car::destroy($id);
    }

    /**
     * Updates a post.
     *
     * @param int
     * @param array
     */
    public function update($id, array $data)
    {
        Car::find($id)->update($data);
    }
}



namespace App\Repositories;

use Illuminate\Support\ServiceProvider;

class RepositoryBackendServiceProvider extends ServiceProvider
{

    public function register()
    {
        $this->app->bind(
            /*
            * Register your Repository classes and interface here
            **/

            'App\Repositories\CarRepositoryInterface',
            'App\Repositories\CarRepository'
        );
    }
}




namespace App\Http\Controllers;

use App\Car;
use App\Repositories\CarRepositoryInterface;

class CarController extends Controller
{
    protected $car;

    public function __construct(CarRepositoryInterface $car)
    {
        $this->$car = $car;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = $this->car->all();

        return $data;
    }
    
}

php artisan vendor:publish --provider="Salman\RepositoryPattern\RepositoryPatterServiceProvider"

php artisan make:repo ModelName
Repositories/RepositoryBackendServiceProvider.php