PHP code example of guardian360 / repository

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

    

guardian360 / repository example snippets




namespace App\Repositories;

use App\User;
use Guardian360\Repository\AbstractRepository as Repository;

class UserRepository extends Repository
{
    /**
     * Specify the model's class name.
     *
     * @return string
     */
    public function model()
    {
        return User::class;
    }
}



namespace App;

use Illuminate\Database\Eloquent\Model

class User extends Model
{
    //
}



namespace App\Http\Controllers;

use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * @var \App\Repositories\UserRepository
     */
    protected $users;

    /**
     * @return void
     */
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    /**
     * Display a listing of the users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()->json($this->users->all());
    }
}

$this->users->all();

$this->users->find(1);

$this->users->findBy('name', 'Joe');

$this->users->findAllBy('role', 'admin');
$this->users->findAllBy('role', ['admin', 'guest']);

$this->users->create($request->all());

$this->users->update($user, $request->all());
$this->users->update(1, $request->all());

$this->users->delete($user);
$this->users->delete(1);
$this->users->delete([1, 2]);



namespace App\Specifications;

use Guardian360\Repository\Contracts\SpecificationContract as Specification

class UserIsAdmin implements Specification
{
    /**
     * Apply the specification.
     *
     * @param  mixed  $query
     * @return mixed
     */
    public function apply($query)
    {
        return $query->where('role', 'admin');
    }
}



namespace App\Http\Controllers;

use App\Specifications\UserIsAdmin;
use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * @var \App\Repositories\UserRepository
     */
    protected $users;

    /**
     * @return void
     */
    public function __construct(UserRepository $user)
    {
        $this->users = $users;
    }

    /**
     * Display a listing of the admins.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $admins = $this->users->pushSpec(new UserIsAdmin)->all();

        return response()->json($admins);
    }
}