PHP code example of morningtrain / laravel-resources

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

    

morningtrain / laravel-resources example snippets




namespace MorningTrain\Laravel\Resources\Operations\Eloquent;

use MorningTrain\Laravel\Resources\Support\Contracts\EloquentOperation;
use MorningTrain\Laravel\Resources\Support\Pipes\Eloquent\QueryToCollection;
use MorningTrain\Laravel\Resources\Support\Pipes\Eloquent\QueryModel;
use MorningTrain\Laravel\Resources\Support\Pipes\TransformToView;

class Index extends EloquentOperation
{

    const ROUTE_METHOD = 'get';

    protected function beforePipes()
    {
        return [

            /**
             * Takes filters and model name as parameters and returns a new query object
             */
            QueryModel::create()->model($this->model)->filters($this->getCachedFilters()),

            /**
             * Trigger `get` on the query and returns the resulting collection
             */
            QueryToCollection::create(),

            /**
             * Transform the collection by applying any appends to each model in the entry
             */
            TransformToView::create()->appends($this->appends, $this->overwrite_appends),
        ];
    }

}




namespace App\Http\Operations\Api;

use MorningTrain\Laravel\Resources\Support\Contracts\EloquentResource;
use MorningTrain\Laravel\Resources\Operations\Eloquent\Index;
use MorningTrain\Laravel\Filters\Filter;
use App\Models\User as Model;

class Users extends EloquentResource
{

    protected $model = Model::class;
    
    public static $operations = [
        Index::class,
    ];

    public function getFilters(){
        return [
            Filter::paginate(),
        ];
    }

}




namespace App\Http\Operations\Api\Users;

use MorningTrain\Laravel\Resources\Operations\Eloquent\Index as Operation;
use MorningTrain\Laravel\Filters\Filter;
use App\Models\User as Model;

class IndexUsers extends Operation
{

    protected $model = Model::class;

    public function getFilters(){
        return [
            Filter::paginate(),
        ];
    }

}


\MorningTrain\Laravel\Resources\Support\Pipes\Eloquent\TriggerOnModel::create()
    ->trigger('someModelMethod')

\MorningTrain\Laravel\Resources\Support\Pipes\Eloquent\TriggerOnModel::create()
    ->trigger(
        fn(Model $model) => $model->someModelMethod()
    )

\MorningTrain\Laravel\Resources\Support\Pipes\ToResourceView::create()
    ->view(MyResourceView::class)

public function readOperation(Read $read)
{
    $read->model(MyModel::class)
         ->resourceView(MyModelResource::class);
}

\MorningTrain\Laravel\Resources\Support\Pipes\Context\SetEnv::create()
    ->environment(['env' => 'variable'])

\MorningTrain\Laravel\Resources\Support\Pipes\Pages\BladeView::create()
    ->path('path.to.blade.view')
    ->parameters(['blade' => 'parameters'])

\MorningTrain\Laravel\Resources\Support\Pipes\Pages\RespondWithPageEnv::create()