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

[8.x] Add trait for models to detect their api resources and add some resource sugar #35515

Closed
wants to merge 6 commits into from
Closed

Conversation

grantholle
Copy link

@grantholle grantholle commented Dec 7, 2020

Borrowing from the new factory syntax, this PR adds a trait which can be added to models to detect the model's api resource. It also adds sugar around generating resource objects, both for single model instances and a collection of models.

This is based on an idea I added last week. I haven't added all the features (basically just alternative resource "states") that I mention in the idea issue, but the sugar is achieve while not introducing any breaking changes to the current way of creating resources.

By adding the Illuminate\Http\Resources\HasResource to a model, the resource can be created by using a few different fluent syntaxes.

Say we have a Post model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\HasResource;

class Post extends Model
{
    use HasResource;
    // 
}

And assume we have a PostResource object found in the typical App\Http\Resources namespace (this supports the naming convention of App\Http\Resources\Post or App\Http\Resources\PostResource, it looks for the existence of either):

use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

We can now create the resource without referencing the resource class directly:

$post = new Post();

// toResource
$post->toResource();

// Using static methods
Post::resource($post);

// or 
Post::resource()->for($post);

This supports collections of models:

Post::resource($postCollection);
// or
Post::resource()->for($postCollection);

Some additional tests are needed before coverage is adequate.

@grantholle grantholle changed the title Add trait for models to detect their api resources and add some resource sugar [8.x] Add trait for models to detect their api resources and add some resource sugar Dec 7, 2020
@driesvints
Copy link
Member

@grantholle Taylor doesn't reviews draft PRs so please mark this as ready if you want it reviewed.

@grantholle grantholle marked this pull request as ready for review December 14, 2020 01:36
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

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.

None yet

5 participants