Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Define the model, route and entity name strings inside the CrudResource, not the CrudController #4

Open
tabacitu opened this issue Jun 30, 2022 · 0 comments

Comments

@tabacitu
Copy link
Collaborator

Feature Request

What I am trying to achieve

What if we could define EVERYTHING we need to define for a CRUD, inside the CrudResource? If you prefer to define a CrudResource, I don't really see a reason why you would split your definition across multiple files:

So what if... if you're using CrudRequest... the CrudController is kind of optional? Only there in case you need to do custom stuff, add things to the setup of a particular operation or override how an operation does its thing?

How I see it implemented

To achieve that, we need to move this from the CrudController:

// BookCrudController.php

    public function setup()
    {
-        CRUD::setModel(\App\Models\Book::class);
-        CRUD::setRoute(config('backpack.base.route_prefix') . '/book');
-        CRUD::setEntityNameStrings('book', 'books');
-
        $this->crudResource = new BookCrudResource();
    }

to the CrudResource:

// BookCrudResource.php


class BookCrudResource extends CrudResource
{
    protected $model = \App\Models\Book::class;
    protected $route = config('backpack.base.route_prefix') . '/book';
    protected $entityNameSingular = 'book';
    protected $entityNamePlural = 'books';

    public function fields(): array
    {
        return [
            Text::make('Name')->size(9),
            Number::make('Year')->size(3),
            Textarea::make('Description')->onlyOnForms(),
            Text::make('ISBN'),
        ];
    }
}

But since some of the above will probably not work (see - calling the config 😅), we'll also need setters/getters for the above, so maybe:

// BookCrudResource.php

class BookCrudResource extends CrudResource
{
    protected $model = 'App\Models\Book'; // mandatory
    protected $entityNameSingular = 'book'; // optional, can figure it out from the model name too
    protected $entityNamePlural = 'books'; // optional, can figure it out from the singular too

    // optional
    public function model() 
    {
        return \App\Models\Book::class;
    }

    // optional, would default to using the entity name as URL segment
    public function route() 
    {
        return config('backpack.base.route_prefix') . '/book';
    }

    // optional
    public function entityNameSingular() 
    {
        return 'book';
    }

    // optional
    public function entityNamePlural() 
    {
        return 'books';
    }

    public function fields(): array
    {
        return [
            Text::make('Name')->size(9),
            Number::make('Year')->size(3),
            Textarea::make('Description')->onlyOnForms(),
            Text::make('ISBN'),
        ];
    }
}

What I've already tried to fix it

Nothing, just spitballing.

Would I be able to work on this myself and submit a PR

Sure, before we launch v6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant