PHP code example of shohag-laraql / lara-ql

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

    

shohag-laraql / lara-ql example snippets




namespace App\Models;

use Shohag\Interfaces\LaraQLSerializer;
use Shohag\Models\LaraQLModel;

class Division extends LaraQLModel implements LaraQLSerializer
{

    /**
     * @var one to one model relation
    */
    protected $one2oneFields = [
        [
            'self_key' => 'country_id', // current table foreign key
            'associate_with' => 'country', // request field
            'relative_model' => Country::class // relative model 
        ]
    ];
  
    public function __construct()
    {
        parent::__construct($this);
    }
    
    /**
     * @param NULL
     * @return $mixin
     */
    public function country() {
        return $this->belongsTo(Country::class, 'country_id', 'id')->select(['id', 'name']);
    } 

    /**
     * @param NULL
     * @return Array
     */
    public function serializerFields(): array
    {
        return ['id', 'name','country_id', 'country__name'];
    }

    /**
     * @param NULL
     * @return Array
     */
    public function postSerializerFields(): array
    {
        return ['name','country_id'];
    }

    /**
     * @param NULL
     * @return Array
     */
    public function fieldsValidator(): array
    {
        return [
            'name' => '



namespace App\Http\Controllers;

use App\Models\Division;
use Shohag\Controllers\LaraQLController;

class DivisionController extends LaraQLController
{
    public function __construct(Division $division)
    {
        $this->EntityInstance = $division;
        parent::__construct(); 
    }
}


   /**
   * @param NULL
   * @return array
   */
    public function fieldMutation()
    {
        return [
            [
                'field' => 'code',
                'method' => function($fieldValue) {
                    return (int)$fieldValue;
                }
            ],
            [
                'field' => 'name',
                'method' => function($fieldValue) {
                    return strtoupper($fieldValue);
                }
            ]
        ];
    }