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

Support API Resources #70

Conversation

robin-dongbin
Copy link

@robin-dongbin robin-dongbin commented Sep 6, 2019

resolve #27 #28
Use like:

$user = UserResource::make(User::find(1));
return Inertia::render('Users/Edit', $user)
$users = UserResource::collection(User::paginate());
return Inertia::render('Users/Index', $users)

or

$users = UserCollection::make(User::paginate());
return Inertia::render('Users/Index', $users);

@reinink
Copy link
Member

reinink commented Sep 6, 2019

Hmm, I thought this worked already? I've used API resources. They automatically get converted to JSON, so this shouldn't be needed.

Plus, we also added Arrayable support recently (although it's not tagged yet): #39

I'll tag that now. Can you try this again, on the latest version? I suspect it will work without any changes needed to the library.

@robin-dongbin
Copy link
Author

  1. Passed to render function is an instance of JsonResource,Not instance of Arrayable.So I think it's not work.
  2. Before response,The data not be fully handled.(such as meta,link).

forgive my poor English 😄

@robin-dongbin
Copy link
Author

And I have test many usage.Here is some examples:

$user = User::find(1);
$props = UserResource::make($user);

return Inertia::render('Users/Edit', $props)
$user = User::find(1);
$props = UserResource::make($user)->additional(['foo'=>'bar']);

return Inertia::render('Users/Edit', $props)

And the with method in your resource class is works well.

Notice that if you want to append other data to a collection.
there are two ways:

  1. use additional
  2. defined with method in your resource collection.

defined in a resource and use UserResource::collection is not works

@sebastiandedeyne
Copy link
Contributor

Before response,The data not be fully handled.(such as meta,link).

This is true, and a super annoying thing with Laravel resources. When calling toArray you lose meta and pagination data.

It's debatable whether Inertia should cast this for the user though, since some people might expect to only get the data when passing down an Eloquent resource collection.

I personally would be very happy with this change, but I don't know what others are doing with resources; it might be to opinionated.

.gitignore Outdated Show resolved Hide resolved
@thoresuenert
Copy link
Contributor

This PR will save time and a Collection class if you go the Laravel resource route.

In our team the resource is a good api for transforming data, I like it a lot.

@grantholle
Copy link

grantholle commented Sep 24, 2019

While I would agree that it's "opinionated," I think this fits with Laravel's "convention over configuration" approach. And since this is a package for Laravel I think this would be really great to include, especially since it's such a simple change. It seems really natural to me to use API Resources when passing data to the components since this "hybrid" approach is trying to take the best of both worlds.

Leveraging API Resources with Inertia would 💯, thanks for taking the time for this

@@ -48,6 +49,10 @@ public function getVersion()

public function render($component, $props = [])
{
if ($props instanceof JsonResource) {

Choose a reason for hiding this comment

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

So in my case, I would be passing an array of props, with some being an instance of JsonResource.

It would need to iterate each of the props to resolve them as well. It may be worth rethinking this a little...

@lexdewilligen
Copy link

@reinink Any updates on this? I need the resource meta, so I'd love to see one of the solutions merged..

@reinink
Copy link
Member

reinink commented Jan 2, 2020

So, I've added support for "Responsable" props: 2b966ac

As you can see, my approach here is almost identical to this PR. However, instead of checking for a JsonResource object, I'm checking for the more generic Responsable object. This makes it useful in other situations as well.

I've tested this with both single resource responses and collection resource responses, and it works great.

Thanks, either way, for submitting this PR @winter-ice. 👍

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.

support for API Resources?
7 participants