laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

[Proposal] Get all attributes in a Blade component. #2373

Open tabuna opened 4 years ago

tabuna commented 4 years ago

Hi everyone 👋.

When I create a component for any service, I am faced with the need to pass all possible attributes:

namespace App\View\Components;

use Illuminate\View\Component;

class AnyService extends Component
{
    /**
     * @var Service
     */
    protected $service;

    /**
     * Create a new component instance.
     *
     * @param Service $service
     * @param string  $param1
     * @param string  $param2
     * @param string  $param3
     * @param string  $param4
     */
    public function __construct(
        Service $service,
        string $param1,
        string $param2,
        string $param3,
        string $param4)
    {
        $service
            ->set('param1', $param1)
            ->set('param2', $param2)
            ->set('param3', $param3)
            ->set('param4', $param4);

    }

    /**
     * Get the view/contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|string
     */
    public function render()
    {
        return $this->service->render();
    }
}

This can be quite tedious compared to some obvious challenges. I think it would be cool if you could get all the passed attributes in bulk:

/**
 * Create a new component instance.
 *
 * @param mixed ...$attributes
 */
public function __construct(...$attributes)
{
    //...
}

What do you think about this?

ahinkle commented 4 years ago

Duplicate of https://github.com/laravel/ideas/issues/2130.

tabuna commented 4 years ago

Hey @ahinkle

I don't think this is duplication. In #2130, the user suggests not using a constructor to specify attributes. This request is aimed specifically at obtaining dynamic attributes that should not be declared but can be obtained. If his proposal is considered and implemented, then this will not, in any way, affect the current issue.