PHP code example of rasyidly / laravel-model-query

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

    

rasyidly / laravel-model-query example snippets


use Rasyidly/ModelQuery/ModelQuery;

class User extends Model
{
    /**
     * This will load Loadable, Searchable, Sortable, and Trashable Traits in single load.
     */
    use ModelQuery;
}

use Rasyidly/ModelQuery/Loadable/Loadable;
use Rasyidly/ModelQuery/Searchable/Searchable;

class User extends Model
{
    use Loadable, Searchable;
}

class Post extends Model
{
    use ModelQuery;

    public $loadable = [
        'author.profile', 'commentable'
    ];

    public function author (): BelongsTo { ... }
}

// posts?load=author,commentable

$query = ['author', 'commentable'];

$posts = Post::loadable($request->query('load'))->get();

// posts?load=author.profile

$post = Post::find(1);

$query = ['author.profile'];
$post = $post->loadable($request->query('load'));

class Post extends Model
{
    use ModelQuery;

    public $searchable = [
        'title', 'description', 'author.name'
    ];

    public function author (): BelongsTo { ... }
}

// posts?search=Hello

$posts = Post::searchable($request->query('search'))->get();

class Post extends Model
{
    use ModelQuery;

    public $sortable = [
        'name'
    ];
}

// posts?sort=name,asc

$posts = Post::sortable($request->query('sort'))->get();

// posts?trash=only

$posts = Post::trashable($request->query('trash'))->get();