Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

[v6][Idea] Maybe we eliminate CrudTrait?! #413

Open tabacitu opened 1 year ago

tabacitu commented 1 year ago

Feature Request

What's the feature you think Backpack should have?

Do we really need the CrudTrait to be added to a Model, in order to work with it? It's one extra step when creating an admin panel... and I don't really like it. It feel like you're bloating up your model. And the stuff it adds... isn't exactly stuff that should be on the Model anyway:

trait CrudTrait
{
    use HasIdentifiableAttribute;
    use HasEnumFields;
    use HasRelationshipFields;
    use HasUploadFields;
    use HasFakeFields;
    use HasTranslatableFields;

    public static function hasCrudTrait()
    {
        return true;
    }
}

It's admin panel stuff, not Model stuff.

Have you already implemented a prototype solution, for your own project?

No, but I've just thought of a way to possibly eliminate it. What I don't know is... how much of a breaking change this would be. But if I'm right... it could even be a non-breaking change... 🤯 Or close to it.

What if when we do

        CRUD::setModel(\App\Models\User::class);

that method doesn't just do

        $this->model = new $model_namespace();

but instead... passed that model... to passthrough Model? So we'd have something like:

<?php

namespace Backpack\CRUD\app\Library\CrudPanel;

class CrudModel
{
    use HasIdentifiableAttribute;
    use HasEnumFields;
    use HasRelationshipFields;
    use HasUploadFields;
    use HasFakeFields;
    use HasTranslatableFields;
    use Macroable; // see what I did here? 😉

    public $model;

    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    public function __call($method, $parameters)
    {
        return $this->model->{$method}(...$parameters);
    }
}

Hmm... then all the methods in those traits would need to call $this->model instead of $this or self or static. So I guess it would be a breaking change... But... maybe it's worth it?

What would the upgrade guide say? Probably:

Do you see this as a core feature or an add-on?

Core but it's a SHOULD/COULD.

tabacitu commented 1 year ago

@pxpm let me know what you think about this idea, if it has any merit in your eyes... if it's worth doing. Feel like a good change from OUR perspective (maintainers), but a pretty selfish one because it doesn't really bring any benefit to the developer. Well I guess:

PROs:

CONs:

tabacitu commented 1 year ago

Holy fucking shit. I've just discovered mixins are a thing 🤯 https://liamhammett.com/laravel-mixins-KEzjmLrx

We could go that route too.