PHP code example of axn / laravel-repository

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

    

axn / laravel-repository example snippets


namespace App\Repositories;

use Axn\Repository\Eloquent\EloquentRepository;
use App\Models\User;

class EloquentUserRepository extends EloquentRepository
{
    public function __construct(User $model)
    {
        parent::__construct($model);
    }
}

// Colonnes sous forme de tableau :
$users = $userRepository->getAll([
    'username',
    'email'
]);

// Ou bien sous forme de chaîne de caractères :
$users = $userRepository->getAll('username, email');

// Avec sélection dans les relations :
$users = $userRepository->getAll([
    'username',
    'email',
    'roles.display_name',
    'roles.permissions.display_name'
]);

$users = $userRepository->getAllBy(['email' => '[email protected]']);

// Avec critères de filtrage sur les relations (ici seront récupérés uniquement
// les utilisateurs ayant le rôle "admin") :
$users = $userRepository->getAllBy(['roles.name' => 'admin']);

$users = $userRepository->getAllBy([
    'email LIKE' => '%@axn.fr'
]);

// IS NOT NULL
$users = $userRepository->getAllBy([
    'email NOT_EQUAL' => null
]);

$users = $userRepository->getAll(null, 'date_inscription desc, lastname, firstname');

// App\Repositories\EloquentUserRepository

public function getAllWithTrashed($columns = null, $order = null, $limit = null, $offset = null)
{
    $query = $this->model()->withTrashed();

    return $this->filter($query, [], $columns, $order, $limit, $offset)->get();
}

public function getAllActive($columns = null, $order = null, $limit = null, $offset = null)
{
    $query = $this->query()->where('active', 1);

    return $this->filter($query, [], $columns, $order, $limit, $offset)->get();
}

public function getAllForDataTable(array $where = [])
{
    $query = $this->query()
        ->join('profils', 'profils.id', '=', 'users.profil_id')
        ->select([
            'users.id',
            'users.username',
            'users.email',
            'profils.name'
        ]);

    if (!empty($where['profil_id'])) {
        $query->where('profils.id', $where['profil_id']);
    }

    return $query->getQuery()->get();
}