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

Allow passing Arrayable objects #39

Merged
merged 1 commit into from
Sep 4, 2019
Merged

Allow passing Arrayable objects #39

merged 1 commit into from
Sep 4, 2019

Conversation

bonzai
Copy link
Contributor

@bonzai bonzai commented Jun 14, 2019

Instead of manually passing an array with data:

class EventsController extends Controller
{
    public function show(Event $event)
    {
        return Inertia::render('Event', [
            'event' => $event->only('id', 'title', 'start_date', 'description'),
        ]);
    }
}

we can use an Arrayable object (for example, like Collection) to pass the data:

use Illuminate\Contracts\Support\Arrayable;

class EventProps implements Arrayable
{
    public $event;

    public function __construct(Event $event)
    {
        $this->event = $event;
    }

    public function toArray()
    {
        return ['event' => $this->event->only('id', 'title', 'start_date', 'description')];
    }
}

class EventsController extends Controller
{
    public function show(Event $event)
    {
        return Inertia::render('Event', new EventProps($event));
    }
}

This way we can reuse the logic and simplify the code.

Similar functionality is already present in Laravel:

Copy link
Contributor

@sebastiandedeyne sebastiandedeyne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@reinink reinink merged commit f82586a into inertiajs:master Sep 4, 2019
@reinink
Copy link
Member

reinink commented Sep 4, 2019

Thanks for the contribution!

@reinink reinink mentioned this pull request Sep 6, 2019
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

Successfully merging this pull request may close these issues.

3 participants