PHP code example of binarcode / laravel-restable

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

    

binarcode / laravel-restable example snippets



namespace App\Models;

use BinarCode\LaravelRestable\HasRestable;
use BinarCode\LaravelRestable\Restable;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model implements Restable
{
    use HasRestable;
}

class Dream extends Model implements Restable
{
    use HasRestable;
    
    public static array $search = [
        'name',
    ];
}

use App\Models\Dream;
use Illuminate\Http\Request;

class DreamController extends Controller
{
    public function index(Request $request)
    {
        $dreams = Dream::search($request);
        
        return response()->json($dreams->paginate());
    }
}

use BinarCode\LaravelRestable\Types;

class Dream extends Model implements Restable
{
    use HasRestable;
    
    public static array $match = [
        'id' => Types::MATCH_ARRAY,
        'name' => Types::MATCH_TEXT,
    ];
}

$query->whereIn('id', [1, 2, 3])->where('name','=', 'happy');

class Dream extends Model implements Restable
{
    use HasRestable;
    
    public static array $sort = [
        'id',
        'name',
    ];
}

class Dream extends Model implements Restable
{
    use HasRestable;
    
    public static function sorts(): array
    {
        return [ 'id', 'name' ];
    }

    public static function matches(): array
    {
        return [
            'id' => 'int',
            'name' => 'string',
        ];
    }

    public static function searchables(): array
    {
        return ['name'];
    }
}

use BinarCode\LaravelRestable\Filters\MatchFilter;class Dream extends Model implements Restable
{
    use HasRestable;

    public static function matches(): array
    {
        return [
            'something' => MatchFilter::make()->resolveUsing(function($request, $query) {
                   // filter it here
            }),
            'name' => 'string'
        ];
    }
}

use BinarCode\LaravelRestable\Filters\MatchFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class MatchSomething extends MatchFilter
{
        public function apply(Request $request, Builder $query, $value): Builder
        {
            // your filters
            return $query->where('a', $value);
        }

}

class Dream extends Model implements Restable
{
    use HasRestable;

    public static function matches(): array
    {
        return [
            'something' => MatchSomething::make(),
            'name' => 'string'
        ];
    }
}