PHP code example of wendelladriel / laravel-more

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

    

wendelladriel / laravel-more example snippets




namespace App\Repositories;

use App\Models\User;
use WendellAdriel\LaravelMore\BaseRepository;

class UserRepository extends BaseRepository
{
    /**
     * @param User $user
     */
    public function __construct(User $user)
    {
        parent::__construct($user);
    }
}

/**
 * Gets all models
 * 
 * @param array $columns
 * @return Collection
 */
public function getAll(array $columns = self::ALL_COLUMNS): Collection

// GET ALL RECORDS WITH ALL COLUMNS
$this->userRepository->getAll();

// GET ALL RECORDS WITH SPECIFIC COLUMNS
$this->userRepository->getAll(['id', 'email']);

/**
 * Gets all models by the given attribute
 *
 * @param string $attribute
 * @param mixed  $value
 * @param string $compareType
 * @param bool   $withTrash
 * @return Collection
 */
public function getAllBy(string $attribute, $value, string $compareType = '=', bool $withTrash = false): Collection

$this->userRepository->getAllBy('is_active', true);

$this->userRepository->getAllBy('type', ['admin', 'manager'], 'IN');

/**
 * Gets a model by the given attribute
 *
 * @param string $attribute
 * @param mixed  $value
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model|null
 */
public function getBy(string $attribute, $value, string $compareType = '=', bool $withTrash = false): ?Model

$this->userRepository->getBy('id', 1);

$this->userRepository->getBy('email', '%@gmail.com', 'LIKE');

/**
 * Gets a model by the given attribute or throws an exception
 *
 * @param string $attribute
 * @param mixed  $value
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model
 */
public function getByOrFail(string $attribute, $value, string $compareType = '=', bool $withTrash = false): Model

$this->userRepository->getByOrFail('id', 1);

$this->userRepository->getByOrFail('email', '%@gmail.com', 'LIKE');

/**
 * Gets a model by some given attributes
 *
 * @param array  $params
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model|null
 */
public function getByParams(array $params, string $compareType = '=', bool $withTrash = false): ?Model

$this->userRepository->getByParams([
    ['is_active', true],
    ['email', '%@gmail.com', 'LIKE']
])

/**
 * Gets a model by some attributes or throws an exception
 *
 * @param array  $params
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model
 */
public function getByParamsOrFail(array $params, string $compareType = '=', bool $withTrash = false): Model

$this->userRepository->getByParamsOrFail([
    ['is_active', true],
    ['email', '%@gmail.com', 'LIKE']
])

/**
 * Gets all models by some given attributes
 *
 * @param array  $params
 * @param string $compareType
 * @param bool   $withTrash
 * @return Collection
 */
public function getAllByParams(array $params, string $compareType = '=', bool $withTrash = false): Collection

$this->userRepository->getAllByParams([
    ['is_active', true],
    ['email', '%@gmail.com', 'LIKE']
])

/**
 * Updates one or more models
 * 
 * @param string $attribute
 * @param        $value
 * @param array  $updateFields
 * @return int
 */
public function updateBy(string $attribute, $value, array $updateFields): int

// UPDATE SINGLE RECORD
$this->userRepository->updateBy('id', 1, ['email' => '[email protected]']);

// UPDATE MULTIPLE RECORDS
$this->userRepository->updateBy('type', ['owner', 'manager'], ['is_active' => true]);

/**
 * Deletes one or more models
 * 
 * @param string $attribute
 * @param        $value
 * @return mixed
 */
public function deleteBy(string $attribute, $value)

// DELETE SINGLE RECORD
$this->userRepository->deleteBy('id', 1);

// DELETE MULTIPLE RECORDS
$this->userRepository->deleteBy('type', ['owner', 'manager']);

/**
 * Creates a new model
 *
 * @param array $args
 * @return Builder|Model
 */
public function create(array $args)

$this->userRepository->create([
    'name' => 'John Dee',
    'email' => '[email protected]',
    'is_active' => true,
])

/**
 * Disables a named global scope
 *
 * @param string $scopeName
 * @return BaseRepository
 */
public function disableGlobalScope(string $scopeName): BaseRepository

$this->userRepository->disableGlobalScope('active-users');

/**
 * Enables a named global scope
 *
 * @param string $scopeName
 * @return BaseRepository
 */
public function enableGlobalScope(string $scopeName): BaseRepository

$this->userRepository->enableGlobalScope('active-users');

/**
 * Gets the table for the base model of the repository
 *
 * @return string
 */
protected function getTable(): string

$usersTable = $this->userRepository->getTable();

/**
 * Builds a new query
 *
 * @param array|string[]|string $columns
 * @return Builder
 */
protected function newQuery(...$columns): Builder

$this->userRepository->newQuery('id', 'email')
    ->where('is_active', true)
    ->get();

php artisan vendor:publish --provider="WendellAdriel\LaravelMore\LaravelMoreServiceProvider" --tag=config