PHP code example of apichef / laravel-request-to-eloquent

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

    

apichef / laravel-request-to-eloquent example snippets


namespace App;

use App\Comment;
use App\Tag;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $dates = [
        'published_at',
    ];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }

    public function scopeDraft(Builder $builder)
    {
        return $builder->whereNull('published_at');
    }
}

namespace App\Queries;

use App\Post;
use ApiChef\RequestToEloquent\QueryBuilderAbstract;
use Illuminate\Http\Request;

class PostListQuery extends QueryBuilderAbstract
{
    protected function init(Request $request)
    {
        return Post::query();
    }

    protected $availableIncludes = [
        'comments',
        'tags',
    ];

    protected $availableFilters = [
        'draft',
    ];

    protected $availableSorts = [
        'published_at',
    ];
}

namespace App\Http\Controllers;

use App\User;
use App\Queries\PostListQuery;

class DashboardController extends Controller
{
    public function index(PostListQuery $postListQuery)
    {
        return $postListQuery
            ->parseAllowedIncludes([
                'comments',
                'tags',
            ])
            ->get()
            ->toArray();
    }
}
bash
$ php artisan vendor:publish --provider="ApiChef\RequestQueryHelper\RequestQueryHelperServiceProvider"