PHP code example of cao-jiayuan / laravel-api

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

    

cao-jiayuan / laravel-api example snippets




namespace App\Exceptions;
//......
use CaoJiayuan\LaravelApi\Foundation\Exceptions\Traits\ExceptionRenderer;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
//......

class Handler extends ExceptionHandler
{
    use ExceptionRenderer;
    //......
    public function render($request, Exception $exception)
    {
        return $this->renderException($request, $exception);
    }
}



namespace App;

use CaoJiayuan\LaravelApi\Database\Eloquent\Helpers\Pipelineable;
use Illuminate\Database\Eloquent\Model;

class Foo extends Model {
    
    use Pipelineable;
    
    public function relate() {
        return $this->belongsTo(App\Bar::class);
    }
}

\App\Foo::pipeline('with:relate|select:id,name');//equals \App\Foo::with('relate')->select(['id','name'])->get();
\App\Foo::join('baz', 'foo_id', '=', 'baz_id')
          ->pipeline('with:relate|select:id,name,baz.title as baz|get|pluck:name|random');




dummy([
   'id' => 1,
   'name|name',
   'address|address',
   'data|list:2' => [
        'id|1+1',   
        'name|name',   
    ]
]);

     
      dummy('rand|1,100');// return random number between 1 and 100
      dummy('rand|true,false');
      dummy([
        'data|list:10|rand:1,5' => [// working with pipeline, generate 10 items, randomly take 1-5 item[s]
             'id|1+1',   
            'name|name',
         ]
      ]);
    

     
      dummy([
        'data1|db:users' => function(\Illuminate\Database\Query\Builder $builder) {
              /// database query
            $builder->select(['id', 'name']);
            /// ......
         },
         'data2|db:articles,2', /// randomly take 2 rows from table articles 
         'data3|db:connection1.articles,2', /// specified connection
         'data4|db:articles,10|pick:2' /// working with pipeline
      ]);
    


namespace App\Http\Controllers;

use CaoJiayuan\LaravelApi\Http\Request\RequestHelper;

class FooController extends Controller
{
    use RequestHelper;
    //
    
    public function post(){
        $data = $this->getValidatedData([
            'name' => 'er],
        ], ['name.
CaoJiayuan/LaravelApi/Foundation/Exceptions/Traits/ExceptionRenderer.php
getValidatedData(array $rules, array $messages = [], array $customAttributes = [])