PHP code example of dinkara / repobuilder

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

    

dinkara / repobuilder example snippets


Dinkara\RepoBuilder\RepositoryBuilderServiceProvider::class,
 bash
php artisan vendor:publish --provider="Dinkara\RepoBuilder\RepositoryBuilderServiceProvider"
 bash
php artisan list
 bash
php artisan make:repo User
 php
    public function register()
    {
    	/*
	   $repos represents array of all  you created with this library	
	*/
        $repos = array(
            'User',  
        );
        
        foreach ($repos as $idx => $repo) {            
            $this->app->bind("App\Repositories\\{$repo}\I{$repo}Repo", "App\Repositories\\{$repo}\\Eloquent{$repo}");
        }
     }
 bash
php artisan make:repo User --migation
 bash
php artisan make:repo User --model
 php


namespace App\Http\Controllers;

use App\Repositories\User\IUserRepo;

class UserController extends Controller
{
    protected $userRepo;
    
    public function __construct(IUserRepo $userRepo) {
        $this->userRepo = $userRepo;
    }
	
    public function show($id)
    {  
        if($item = $this->repo->find($id)){
		return response()->json($item->getModel());
	}
		
	return response('Not Found', 404);	
     }
     
    public function update(Request $request, $id)
    {  
        $data = $request->all();
        if($this->userRepo->find($id)->update($data)){
		return response("Ok", 200);
	}
	
	return response("Failed", 500);
     }
	
	/*
		...
		Your code
		...
	*/
	
}
 php


namespace Dinkara\RepoBuilder\Repositories;

interface IRepo {

    function model();
	
    function getModel();

    function firstOrNew($where);

    function firstOrCreate($where);

    function fill($fields);
    
    function find($id);
    
    function all();
    
    function paginateAll($perPage = 20);

    function create($fields);

    function findAndUpdate($id, $fields);

    function update($fields);
    
    function save();
    
    function delete();
    
    function __call($name, $arguments);
}
 php
$this->userRepo->findByEmail($email);
 php


namespace App\Repositories\User;

use App\Repositories\IRepo;
/**
 * Interface UserRepository
 * @package App\Repositories\User
 */
interface IUserRepo extends IRepo { 
    /**
     * Function that creates new user
     * @param type $fields
     */
    function register($fields);
    
	/*
		...
		Your custom functions
		...
	*/
}
 php


namespace App\Repositories\User;

use App\Repositories\EloquentRepo;
use App\Models\User;

class EloquentUser extends EloquentRepo implements IUserRepo {


    /**
     * Configure the Model
     * */
    public function model() {
        return new User;
    }
    
    public function register($fields) {

        $fields["confirmation_code"] = str_random(30);

        $result = $this->create($fields)->attachRole("customer");

        return $this->finalize($result);
    }


    private function attachRole($role) {
        if (!$this->model) {
            return false;
        }

        $result = $this->model->roles()->attach($role);
        
        return $this->finalize($this->model);
    }   

}