PHP code example of imunew / laravel-database-queries

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

    

imunew / laravel-database-queries example snippets


namespace App\Database\Queries\User;

use App\Models\User;
use Imunew\Laravel\Database\Queries\Query;
use RuntimeException;

/**
 * Class SameName
 * @package App\Database\Queries\User
 *
 * @mixin User
 */
class SameName extends Query
{
    /**
     * SameName constructor.
     * @param array $parameters
     * @param array $with
     */
    public function __construct(array $parameters, array $with = [])
    {
        parent::__construct(User::class, $parameters, $with);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function validateParameters(array $parameters, ?string &$errorMessage)
    {
        if (!array_key_exists('name', $parameters)) {
            $errorMessage = 'The parameter \'name\' must not be empty.';
            return false;
        }
        return true;
    }

    /**
     * {@inheritdoc}
     */
    protected function buildQuery(array $parameters)
    {
        $this->whereName($parameters['name']);
        return $this;
    }
}

use App\Database\Queries\User\SameName;

function findByName(string $name) {
    $query = new SameName(['name' => $name]);
    return $query->build()->get();
}

use App\Database\Queries\User\SameName;
use App\Database\Queries\User\SameEmail;
use Imunew\Laravel\Database\Queries\Chain;

function firstByNameAndEmail(string $name, string $email) {
    $chain = Chain::all([
        new SameName(['name' => $name]), 
        new SameEmail(['email' => $email])
    ]);
    return $chain->build()->first();
}
bash
$ php artisan make:database-query {name} --model={model}