PHP code example of zlt / laravel-api-auth

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

    

zlt / laravel-api-auth example snippets


  PostService::get([
    'startDate' => '2022-01-01',
    'endDate' => '2022-06-01',
    'orderBy' => 'date',
    'isDesc' => true,
  ])
  



return [
    /*
     * If true, `api/login` and `api/register` routes will be registered.
     */
    'shouldIncludeRoutes' => true,

    /*
     * Define your auth model
     */
    'authUser' => App\Models\User::class,

    /*
     * If true, action will try to create token in registration and appending token in login.
     */
    'enableSanctum' => true,

    /*
     * In some cases, you might want to use other column than `email`
     */
    'username' => 'email',

    /*
     * Validation rule for password in registration
     */
    'password:rule' => '

    $values = ['email'=>'[email protected]','password' => '12345678'];
    $login = new Zlt\LaravelApiAuth\Actions\Login;
    $response = $login(values: $values);
    

    $values = ['email'=>'[email protected]','password' => '12345678'];
    $register = new Zlt\LaravelApiAuth\Actions\Register;
    $response = $register(values:$values);
    

  $values = ['email'=>'[email protected]'];
  $delete = new Zlt\LaravelApiAuth\Actions\Delete;
  $response = $delete($values);
  



namespace App\Services;

use App\Models\Post;
use Zlt\LaravelApiAuth\Enums\Operator;
use Zlt\LaravelApiAuth\Services\BaseService;
use Zlt\LaravelApiAuth\Support\QueryableColumn;

/**
 * @method static ApiResponse get(array $values);
 * @method static ApiResponse count(array $values);
 */
class PostService extends BaseService
{
    public function __construct()
    {
        $this->registerQueryColumn(QueryableColumn::from(
            'date',
            ['startDate', 'endDate'],
            'date_format:Y-m-d|

PostService::get([
    'startDate' => '2022-01-01',
    'endDate' => '2022-06-01',
    'orderBy' => 'date',
    'isDesc' => true,
]);

PostService::count([
    'startDate' => '2022-01-01',
    'endDate' => '2022-06-01',
    'orderBy' => 'date',
    'isDesc' => true,
]);

  class YourService extends Zlt\LaravelApiAuth\Services\BaseService{
  
    static function getInstance(): static
    {
       
    }
  }
  

  // Pass to constructor
  static function getInstance(): static
  {
     return new static(YourModel::query());
  }
  
  // Inside constructor
  public function __construct()
  {
     parent::__construct(YourModel::query());
  }
  

    PostService::get([
        'orderBy' => 'date',
        'isDesc' => true,
        'limit' => 10,
        'offset' => 0,
        'hiddenFields' => ['id','created_at','updated_at'],
        'selectedFields' => ['title','date'],
    ]);
    

  // Register queryable column in constructor
  class YourService extends Zlt\LaravelApiAuth\Services\BaseService{
  
    public function __construct()
    {
        $this->registerQueryColumn(QueryableColumn::from(
            'date', // DB Column
            ['startDate', 'endDate'], // Request Parameters
            'date_format:Y-m-d|

  public function __construct()
  {
      $this->registerQueryColumn(QueryableColumn::from(
          'date', // DB Column
          ['startDate', 'endDate'], // Request Parameters
          'date_format:Y-m-d|->startOfDay()->timestamp;
                }
                return $date->endOfDay()->timestamp;
            }
      ));
      parent::__construct(YourModel::query());
  }
  

use Zlt\LaravelApiAuth\Utils\CanCache;

class YourClass {

    use CanCache;
  
    public function yourMethod(Request $request){
        if($this->checkIfCacheKeyExists($request)){
            return $this->getCacheData($request);
        }
        $response = $this->computeData();
        $this->storeCache($request,$response);
        return $response;
    }
}
bash
$ php artisan vendor:publish --provider="Zlt\LaravelApiAuth\LaravelApiAuthServiceProvider"