Skip to content

Latest commit

 

History

History
157 lines (123 loc) · 3.88 KB

resources.md

File metadata and controls

157 lines (123 loc) · 3.88 KB
title github order
Resources
3

Resources are basically extra layers around models. Using resources it's easy to build up a CRUD workflow for a model, using fields, filters, actions or widgets.

Creating Resources

You may create a Resource class using the root:resource artisan command. It requires only a name as its parameter and generates the resource class for the model:

php artisan root:resource PostResource

# or

php artisan root:resource CustomPostResource --model=Post

Registering Resources

You may register your resources by using the RootServiceProvider which is published and automatically registered when the root:install artisan command is called.

namespace App\Providers;

use App\Root\Resources\PostResource;
use App\Root\Resources\UserResource;
use Cone\Root\RootApplicationServiceProvider;

class RootServiceProvider extends RootApplicationServiceProvider
{
    /**
     * The resources.
     */
    protected function resources(): array
    {
        return [
            new UserResource(),
            new PostResource(),
        ];
    }
}

Configuration

You may allow or disallow interaction with resources. To do so, you can call the authorize method on the resource instance:

$resource->authorize(static function (Request $request): bool {
    return $request->user()->isAdmin();
});

Fields

For the detailed documentation visit the fields section.

Fields are handlers for the model attributes. They are responsible for saving and displaying the given attribute of the resource model. You can easily define fields on your resource by using the fields method:

use Cone\Root\Fields\ID;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Illuminate\Http\Request;

class PostResource extends Resource
{
    /**
     * Define the fields for the resource.
     */
    public function fields(Request $request): array
    {
        return [
            ID::make(),
            Text::make('Title'),
        ];
    }
}

Filters

For the detailed documentation visit the filters section.

Filters are responsible for transforming the current request to a database query. You can easily define filters on your resource by using the filters method:

use App\Root\Filters\Category;
use Cone\Root\Resources\Resource;
use Illuminate\Http\Request;

class PostResource extends Resource
{
    /**
     * Define the filters for the resource.
     */
    public function filters(Request $request): array
    {
        return array_merge(parent::filters($request), [
            Category::make(),
        ]);
    }
}

Actions

For the detailed documentation visit the actions section.

Actions are responsible for performing a specific action on a set of models. You can easily define actions on your resource by using the actions method:

use App\Root\Actions\Publish;
use Cone\Root\Resources\Resource;
use Illuminate\Http\Request;

class PostResource extends Resource
{
    /**
     * Define the actions for the resource.
     */
    public function actions(Request $request): array
    {
        return [
            Publish::make(),
        ];
    }
}

Widgets

For the detailed documentation visit the widgets section.

Widgets are cards that hold some information or any content you want to display. You can easily define widgets on your resource by using the widgets method:

use App\Root\Widgets\TotalPosts;
use Cone\Root\Resources\Resource;
use Illuminate\Http\Request;

class PostResource extends Resource
{
    /**
     * Define the widgets for the resource.
     */
    public function widgets(Request $request): array
    {
        return [
            TotalPosts::make(),
        ];
    }
}