Eloquent Resources

Funny Laravel docs · Warning: May cause actual learning AND laughter!

Browse funny laravel docs
On this page

Ahoy there, Laravel sailors! Buckle up for an exhilarating journey into the realm of Eloquent API Resources. This section is chock-full of goodies thatโ€™ll make your RESTful dreams come true. Letโ€™s dive right in! ๐ŸŒŠ

Generating Resources (Like a Pro)

You know the drill โ€“ no one wants to code manually all day. Laravel makes it a breeze with a command that generates resources for you. Now, who needs a personal assistant when youโ€™ve got this bad boy? ๐Ÿค–

Concept Overview (Whatโ€™s in it for me?)

Weโ€™re talking about Resource Collections here โ€“ like a buffet of data for your API consumers. You can serve up just the right stuff they need to satisfy their curiosity, without giving away all your secrets! ๐Ÿฝ๏ธ

Writing Resources (Customizing Your Plate)

Now weโ€™re getting to the good part โ€“ customization! With Data Wrapping, Pagination, Conditional Attributes, Relationships, and Meta Data, you can whip up a unique data dish tailored for each consumer. Mix it up, keep โ€˜em guessing! ๐ŸŽจ

JSON:API Resources (For the picky eaters)

But what if your consumers are particular about their JSON format? No worries โ€“ weโ€™ve got JSON:API Resources just for them. You can generate, define attributes and relationships, handle resource type and ID, use sparse fieldsets and includes, and add links and meta data. Talk about a customizable feast! ๐ŸŒฎ

Resource Responses (The icing on the cake)

Last but not least, itโ€™s time to serve up your creation with style! With Laravel, you can craft elegant and powerful API responses that are sure to leave a lasting impression. Bon appรฉtit! ๐Ÿฝ๏ธโœจ

Alrighty then! Letโ€™s dive into the whimsical world of API construction where a little black box named Eloquent models could use some sprucing up before they serve your users a plateful of JSON goodness. You see, sometimes you want to dish out specific attributes to some lucky diners and not others, or perhaps always include certain side dishes (relationships) with every meal. Thatโ€™s where the dapper Eloquent resource classes step in, ready to whisk your models into a gourmet transformation!

Now, you could always convert those Eloquent models or collections into JSON using their trusty toJson methods, but that would be like serving a steak medium-rare when your users were expecting well-done. Eloquent resources offer a more precise and sturdy control over the seasoning (JSON serialization) of your models and their relationships, ensuring that every bite is as delightful as the last!

Letโ€™s get cooking! Oh wait, I mean coding, with this tasty recipe for generating Eloquent resources.

โ€œSweeten up your API game with these easy steps!โ€

  1. Navigate to your project root directory in a terminal or command prompt.
  2. Run the following Artisan command: php artisan make:resource UserResource (Replace โ€œUserโ€ with the name of the model you wish to create a resource for.)
  3. Open the newly created file located at app/Http/Resources/UserResource.php.
  4. Voilร ! You now have a brand-new, spiffy Eloquent resource class at your fingertips. Start customizing it with methods like toArray(), with (for including relationships), and without (for excluding relationships) to create the perfect JSON representation for your models!
  5. To use this resource in your controllers, simply inject it into your methods: public function index(UserResource $userResource) {...}.
  6. Your users will thank you for serving up such a tasty API experience!

Unleashing Resourcefulness! ๐Ÿš€

Ready to become a resourceful superhero? Well, strap on your cape and letโ€™s dive into Laravelโ€™s enchanted world of resources!

To summon a resource class, you gotta yell out the magic words: make:resource! With just that, the Artisan command will appear like magic, whipping up a new resource for you. By default, these shiny new creations will reside in the app/Http/Resources folder of your application. ๐Ÿข

So, if you want to create a UserResource (because letโ€™s face it, who doesnโ€™t need more friends?), just give this incantation a spin:

php artisan make:resource UserResource

Wait for the puff of digital smokeโ€ฆ and ta-da! Your new resource extends the mighty Illuminate\Http\Resources\Json\JsonResource class, making it an even more formidable ally in your quest to conquer APIs! ๐Ÿฆธโ€โ™‚๏ธ๐Ÿ‘ฉโ€๐Ÿš€

Oh, hey there! Ready to embark on a whirlwind adventure through the mesmerizing world of Laravel Resource Collections? Buckle up, my friend, because weโ€™re about to dive deep into the magical land where models transform into glorious collections, and JSON responses become a treasure trove of links and meta-goodness!

First off, letโ€™s talk about individual models. We all love them, right? But sometimes, we need more - enter: Resource Collections! These bad boys are responsible for converting entire ensembles of models into a harmonious symphony of data thatโ€™ll have your JSON responses dancing the tango of relevancy like never before!

To summon a Resource Collection, youโ€™ve got two options. One, whip out your magical --collection flag when creating the resource using:

php artisan make:resource User --collection

Two, if youโ€™re feeling fancy (and why wouldnโ€™t you be?), opt for the elegance of naming your resource with the word โ€œCollectionโ€ appended at the end. Like so:

php artisan make:resource UserCollection

Now, donโ€™t get all flustered - these Resource Collections are actually just extended family members of Illuminate\Http\Resources\Json\ResourceCollection. So, theyโ€™re not entirely unrelated. Phew!

So there you have it! A fun-filled journey through the wondrous world of Laravel Resource Collections. Now go forth and conquer the JSON response landscape with your newfound knowledge!

Resource Revelry! ๐ŸŽ‰๐ŸŽˆ

The Lowdown on Resources and Resource Collections ๐Ÿ“š๐Ÿ”

[๐Ÿ’ก๏ธ] This is a grand tour of resources and resource collections. Itโ€™s highly recommended to check out the rest of this documentationโ€™s sections to really delve into the customization and potent powers that await you in resources!

Before we dive headfirst into all the fantastic features Laravel offers when crafting resources, letโ€™s first take a high-level peek at how resources are utilized within our beloved Laravel. A resource class embodies a single model that requires transformation into a JSON structure. For instance, hereโ€™s a simple UserResource class:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

Every resource class defines a toArray method which returns the array of attributes that should be transformed into JSON when the resource is served as a response from a route or controller method. ๐ŸŽถ And remember, we can access model properties directly with the magic $this variable because a resource class will automatically channel property and method access down to the underlying model for your convenience!

Once defined, these resources may be served up by either a route or a controller. Hereโ€™s an example using a route:

use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/user/{id}', function (string $id) {
    return new UserResource(User::findOrFail($id));
});

For a quicker draw, you can use the modelโ€™s toResource method, which will employ Laravel conventions to automatically discover the modelโ€™s underlying resource:

return User::findOrFail($id)->toResource();

When invoking the toResource method, Laravel will attempt to locate a resource that matches the modelโ€™s name and is optionally suffixed with Resource within the Http\Resources namespace closest to the modelโ€™s namespace. ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ”

If your resource class doesnโ€™t follow this naming convention or is located in a different namespace, you can specify the default resource for the model using the UseResource attribute:

<?php

namespace App\Models;

use App\Http\Resources\CustomUserResource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Attributes\UseResource;

#[UseResource(CustomUserResource::class)]
class User extends Model
{
    // ...
}

Alternatively, you can specify the resource class by passing it to the toResource method:

return User::findOrFail($id)->toResource(CustomUserResource::class);

Resource Collections ๐ŸŽ๐ŸŽ‰

Just when you thought resources couldnโ€™t get any more awesome, Laravel introduces resource collections! A resource collection allows you to easily format a collection of models as JSON. Hereโ€™s an example UserCollection class:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * Transform the collection into a JSON structure.
     *
     * @return array<string, mixed>[]
     */
    public function toArray($request): array
    {
        return [
            'data' => $this->collection,
        ];
    }
}

Now, instead of returning a single resource instance in your controller or route, you can use the transform method to return a collection of resources:

use App\Http\Resources\UserCollection;
use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::all());
});

And thatโ€™s just the tip of the iceberg! Go forth and create, my friends. Happy coding! ๐Ÿš€๐ŸŒŸ๐ŸŽˆ

Resource Palooza! ๐ŸŽ‰

If youโ€™re serving up a delightful smorgasbord of user resources or a pageant-worthy parade of paginated responses, remember to dress them up with the collection method from your resource class when summoning them in your route or controller:

Use the UserResource's finest china, paired with some suave User models for an elegant dinner party! ๐Ÿฅ™๐Ÿด

use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/users', function () {
    return (new UserResource(User::all()))->onAPlatter(); // Fancy, right? โœจ
});

Or if youโ€™re feeling a bit peckish and prefer a quick takeaway, grab the Eloquent collection and whip it into shape with the toResourceCollection method. It knows the drill and will use framework conventions to find the perfect match:

Just shove 'em in the microwave (or something less destructive), and they'll transform into delectable UserResources! ๐Ÿ”๐Ÿ”ฅ

return User::all()->toResourceCollection();

When it comes to toResourceCollection, Laravel plays detective, looking for a resource collection that matches the modelโ€™s name with a Collection suffix in the Http\Resources namespace closest to home.

But what if your resource collection is a rebel without a Closure and goes by a different name or hails from a foreign land? No worries! Use the UseResourceCollection attribute to school Laravel on your resourceโ€™s identity:

<?php

namespace App\Models;

use App\Http\Resources\CustomUserCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Attributes\UseResourceCollection;

#[UseResourceCollection(CustomUserCollection::class)] // Now Laravel knows who's the boss! ๐Ÿ‘‘
class User extends Model
{
    // ...
}

Alternatively, you can still use the toResourceCollection method and explicitly tell it who your resource collectionโ€™s date is:

"I'm with CustomUserCollection now!" they say. ๐Ÿ’ฌ
return User::all()->toResourceCollection(CustomUserCollection::class);

Happy coding, folks! Let the Resource Rumble begin! ๐Ÿฅณโœจ๐ŸŽ‰

Alrighty, letโ€™s spice up Laravelโ€™s documentation on Custom Resource Collections! Here we goโ€ฆ

Custom Resource Collections: The Spicy Side of Data Collection ๐ŸŒถ๏ธ

By default, resource collections are as vanilla as your grandmaโ€™s apple pie. They donโ€™t allow you to add any extra condiments (ahem, metadata) that might be a fine sprinkle on your collection. But fear not, my data-hungry friend! You can create a dedicated salsa stand, I mean resource, to represent your collection:

php artisan make:salsa UserCollection ๐ŸŒฎ

Once the resource collection class has been whipped up, you can easily decide what condiments (metadata) should be served with the main dish:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection as BaseResourceCollection;

class UserCollection extends BaseResourceCollection {
    /**
     * Throw a party! Turn this resource collection into an array.
     *
     * @return array<int|string, mixed>
     */
    public function toArrayWithASideOfGuac(Request $request): array
    {
        return [
            'taco' => $this->collection, // The main dish
            'links' => [ // The condiments
                'self' => 'link-value',
            ],
        ];
    }
}

After youโ€™ve seasoned your resource collection, it can be served up from a route or controller:

use App\Http\Resources\UserCollection;
use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::all());
});

Or, for convenience, you can use the Eloquent collectionโ€™s toSalsaCollection method, which will use framework conventions to automatically discover the modelโ€™s underlying resource collection:

return User::all()->toSalsaCollection();

When invoking the toSalsaCollection method, Laravel will search high and low for a resource collection that matches the modelโ€™s name and is suffixed with Collection within the Http\Resources namespace closest to the modelโ€™s neighborhood. ๐Ÿก

Now, hold your tacos! If youโ€™d like to preserve those precious collection keys while turning them into a resource collection, simply override the toArray method in your custom resource collection:

public function toArrayWithKeys(Request $request): array {
    return parent::toArray($request);
}

Happy coding, and may your collections always be salsa-fresh! ๐ŸŒฎ๐Ÿš€

Ah, the joy of key preservation in Laravel! You know, itโ€™s a bit like maintaining order at a chaotic party - somebodyโ€™s got to do it. ๐Ÿฅณ

In the realm of routing resources, Laravel is a tidy soul, sorting collection keys numerically for you by default. But fear not, for they didnโ€™t forget about those who enjoy a bit of nostalgia! ๐ŸŽง

You can employ the PreserveKeys attribute on your resource class to decide whether to keep the original keys in the collection:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Attributes\PreserveKeys;
use Illuminate\Http\Resources\Json\JsonResource;

#[PreserveKeys]
class UserResource extends JsonResource
{
    // ...
}

When the preserveKeys flag is set to true, the collection keys will remain intact when returned from a route or controller:

use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/users', function () {
    return UserResource::collection(User::all()->keyBy('id'));
});

P.S. Donโ€™t forget to keep those keys unique and happy - nobody wants a key-tastrophe on their hands! ๐Ÿคช๐Ÿฅ„๐Ÿฝ๏ธ

Customizing the Underlying Chicken Coop Class (Ainโ€™t That a Hoot!)

In the barnyard of Laravel, you might find yourself scratching your feathers, wondering how to customize your chicken coop class. Well, buckle up and grab some feed, because weโ€™re about to cluck things up a notch!

Usually, the $this->flock property of a resource collection gets filled with chicks that have been transformed into their individual, sleek, rooster-approved resource class. This fowl-tastic resource class is assumed to be the collectionโ€™s name without the trailing โ€œCoopโ€ portion. But wait! Thereโ€™s more - this class may or may not sport a fancy โ€œResourceโ€ suffix, depending on your pecking order preferences.

For instance, if you have a UserCollection, it will try to convert those clucky user chicks into smart, clean UserResource roosters. To modify this behavior, you can use the Collects attribute on your resource collection:

<?php

namespace App\HenHouse;

use Illuminate\Http\Resources\Attributes\Collects;
use Illuminate\Http\Resources\NestBox\ResourceCollection;

#[Collects(Fowl::class)]
class UserCollection extends ResourceCollection
{
    // ...
}

Now, if youโ€™re feeling cocky, go ahead and customize your resource collection. But remember, thereโ€™s no use crying over spilled corn if things donโ€™t go according to plan โ€“ just grab a fresh nest box and get clucking!

The Resourceful Part of Laravel

[! Attention] If you havenโ€™t given the concept overview a whirl yet, itโ€™s like showing up to a party without knowing the host - embarrassing, but fixable. Fix it, buddy!

Resources are the magicians that turn your models into palatable arrays for our API friends. Each resource carries a secret wand named toArray method, which conjures up your modelโ€™s attributes into an easy-to-digest, API-friendly array:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'birthday' => $this->created_at, // Because who doesn't love a birthday party?
            'lastSeen' => $this->updated_at,  // Keeping track of the last time they danced.
        ];
    }
}

Once youโ€™ve created your resource, itโ€™s ready to be served up straight from a route or controller:

use App\Models\User;

Route::get('/user/{id}', function (string $id) {
    $user = User::findOrFail($id); // We gotta find our guest first.
    return $user->toUserResource();  // And serve them up in style!
});

Alrighty then! Letโ€™s embark on a jolly journey through the magical land of Laravel Relationships. Buckle up, partner, because things are about to get resourceful!

If you fancy bundling related goodies with your response, you can toss โ€˜em into the array spieled out by your resourceโ€™s toArray method. To help you along, weโ€™ll demonstrate this with our trusty companion, the PostResource. Letโ€™s slap those user blog posts onto our resource response like a cowboy slings his trusty six-shooter:

use App\Http\Resources\PostResource;
use Illuminate\Http\Request;

/**
 * Turn this resource into an array suitable for serving.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'posts' => PostResource::collection($this->posts), // Yee-haw! Let's saddle up those posts!
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

Psa

If you want to keep your relationships under wraps until theyโ€™ve been properly lassoed, give the docs on conditional relationships a gander.

Alright, letโ€™s dive into the enchanting world of Laravel Resource Collections!

Resource Collections are like a symphony orchestra where each model is an instrument, and instead of a chaotic cacophony, we get a beautifully structured concert. But wait, unlike the musicians who need individual contracts, our models donโ€™t require their own Resource Collection classes - thanks to Laravelโ€™s magical toResourceCollection method!

use App\Models\User; // Remember, this is your favorite band in the database world

Route::get('/users', function () {
    // This line is the equivalent of saying "Let's hit the stage and play our hits!"
    return User::all()->toResourceCollection();
});

However, if youโ€™re the kind of developer who insists on personalizing your setlist (I mean, who doesnโ€™t?), you can define your own Resource Collection:

<?php

// Backstage, where the magic happens
namespace App\Http\Resources;

use Illuminate\Http\Request; // Our director for this show
use Illuminate\Http\Resources\Json\ResourceCollection; // The playbook

class UserCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'data' => $this->collection, // Our main act
            'links' => [
                'self' => 'link-value', // A little encore for the audience
            ],
        ];
    }
}

Just like their singular counterparts, Resource Collections can take center stage directly from routes or controllers:

use App\Http\Resources\UserCollection; // Our newest album
use App\Models\User; // The band again, but who's keeping track?

Route::get('/users', function () {
    // This line is the equivalent of saying "Give me a drumroll...and cue the new album!"
    return new UserCollection(User::all());
});

And if youโ€™re too lazy to write another album (I mean, who isnโ€™t?), you can use the Eloquent collectionโ€™s toResourceCollection method:

return User::all()->toResourceCollection(); // Laravel's secret weapon - the magical wand!

When using toResourceCollection, Laravel performs a charming hunt for the Resource Collection that best matches your modelโ€™s name, with a delightful suffix of Collection. All within the Http\Resources namespace, closer to where your model lives. Itโ€™s like having an agent who knows all the right people in town! ๐Ÿ˜Š

Alright, letโ€™s dive into the world of data wrapping in Laravel! ๐ŸŽ

By default, when your top-tier resource gets converted into JSON, itโ€™s lovingly swaddled in a โ€œdataโ€ key, transforming your response into something resembling a baby gator nest. Hereโ€™s an example to tickle your fancy:

{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ]
}

Now, if youโ€™re not a fan of the swaddling and prefer your data to be as free-range as a digital chicken in Silicon Valley, you can disable this wrapping by invoking the withoutWrapping method on the base Illuminate\Http\Resources\Json\JsonResource class. To do that, just drop by at your AppServiceProvider (itโ€™s always happy to see new guests) or any other service provider thatโ€™s loaded with every request to your application:

<?php

namespace App\Providers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        // ...
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        JsonResource::withoutWrapping();
    }
}

[๐Ÿšจ CAUTION] The withoutWrapping method only tucks in the outermost response and wonโ€™t remove โ€œdataโ€ keys that youโ€™ve manually added to your own resource collections. So, your baby gator nest may still have a few decorative feathers! ๐Ÿฆ„

Alrighty then! Letโ€™s dive into the dazzling world of Laravelโ€™s Nested Resource wrapping, where your data is swathed in more layers than a freshly baked croissant from a Parisian patisserie. But donโ€™t worry, we wonโ€™t let you end up with a double-wrapped, soggy mess!

You see, you get to decide how your resourceโ€™s relationships are wrapped, like a burrito with an extra layer of foil. If you want all resource collections to be enclosed in a delightful data key, regardless of their nesting depth, just define a resource collection class for each resource and serve the collection within a cozy data embrace:

<?php

namespace App\Http\Resources;

Use your imagination as a maรฎtre d', serving up resources with a side of 'data' key elegance.

use Illuminate\Http\Resources\Json\ResourceCollection;

class CommentsCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return ['data' => $this->collection];
    }
}

Now, you might be scratching your head and wondering if this will result in your outermost resource being wrapped in a double-layered data key. Fret not! Laravel is the caring host that ensures your resources are never accidentally overdressed (unless you ask for extra dressing, of course). So thereโ€™s no need to worry about the tiered complexity of the resource collection youโ€™re transforming.

Bon appรฉtit! Enjoy wrapping those Nested Resources like a pro!

Alrighty then! Letโ€™s talk about Laravelโ€™s Data Wrapping and Pagination shenanigans. When you serve up a paginated collection like a fancy hors dโ€™oeuvre at a posh soiree, Laravelโ€™ll slap it between a swanky data key - even if youโ€™ve asked it to refrain with the withoutWrapping method. Why? Because just like a well-dressed butler, paginated responses always have a bit of extra flair: meta and links keys, brimming with juicy details about the paginatorโ€™s state of affairs.

Hereโ€™s an example of how your data might strut its stuff on the dance floor (or web page):

{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ],
    "links":{
        "first dance": "http://example.com/users?page=1",
        "last call": "http://example.com/users?page=1",
        "prev ballad": null,
        "next cha-cha": null
    },
    "meta":{
        "current song": 1,
        "opening number": 1,
        "final act": 1,
        "venue": "http://example.com/users",
        "act count": 15,
        "interval": 10,
        "total performances": 10
    }
}

Now, doesnโ€™t that make pagination a little less boring? Just remember - while your data is out there mingling with the high-society guests, Laravelโ€™s got its back!

Pagination: The Magical Number Maker!

Who needs a crystal ball when you have Laravelโ€™s pagination? Itโ€™s like Hogwartsโ€™ Sorting Hat, but for your data collections!

Want to pass around some nicely portioned data? Go ahead and hand over a Laravel Paginator instance to your favorite resource or custom collection:

Use the UserResourceFactory, you know, like Cinderella's fairy godmother;
Use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::paginate());
});

Or, if youโ€™re feeling extra magical, use the paginatorโ€™s toResourceCollection() method. Itโ€™ll do all the legwork for you, using good old Laravel conventions to find the resource collection of your paginated model:

return User::paginate()->toResourceCollection();

And voila! Your responses will now come with a glamorous meta and links section, complete with all the juicy pagination details. Imagine it as the fancy invitation to the ball your data always wanted:

{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ],
    "links":{
        "first": "http://example.com/users?page=1",
        "last": "http://example.com/users?page=1",
        "prev": null,
        "next": null
    },
    "meta":{
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://example.com/users",
        "per_page": 15,
        "to": 10,
        "total": 10
    }
}

Customizing the Pagination Information

Feeling like the Ugly Stepsisters of pagination? No worries, you can dress it up to your liking! Customize the links and meta sections with a little bit of magical styling:

$paginator = User::paginate(10);

return new UserCollection($paginator)->addMeta([
    'label' => 'The Hall of Users',
    'path'  => route('users.index'),
]);

return $paginator->toResourceCollection()->addLinks([
    'prevPage' => "<a href='" . $paginator->previousPageUrl() . "'>Go Back</a>",
    'nextPage' => "<a href='" . $paginator->nextPageUrl() . "'>Next Page โ†—๏ธ</a>",
]);

Now, your data gets the red carpet treatment it deserves:

{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ],
    "links":{
        "prevPage": "<a href='http://example.com/users?page=0'>Go Back</a>",
        "nextPage": "<a href='http://example.com/users?page=2'>Next Page โ†—๏ธ</a>"
    },
    "meta":{
        "label": "The Hall of Users",
        "path": "http://example.com/users"
    }
}

Now that your pagination is a hit, prepare for all the data invitations pouring in!

Alrighty then! Fancy yourself a pagination maestro? Well buckle up, buttercup, because weโ€™re about to embark on an adventure through the wild world of custom pagination info! ๐ŸŽข ๐ŸŒด

Now, letโ€™s say you want to add some sass to those humdrum links or meta keys in your pagination response. Well, fear not my dear friend, for Laravel has provided us with a splendiferous method known as paginationInformation. This swell function is just itching to dance on the stage of your resource! ๐Ÿ’ƒ๐Ÿ•บ

But what, you ask, does this magical method do exactly? Well, let me โ€˜splain. You see, our trusty paginationInformation function will gladly accept three parameters: $request, $paginated, and $default. The $paginated data is the main attraction here, while $default is an array that contains the links and meta keys. ๐ŸŽซ

Now, letโ€™s get our hands dirty with a code example:

/**
 * Customize the pagination information for the resource - fancy, huh?
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  array  $paginated - data, data, data!
 * @param  array  $default - keys and stuff.
 * @return array
 */
public function paginationInformation($request, $paginated, $default)
{
    $default['links']['custom'] = 'https://example.com/lets-party'; // Yippee! A custom URL for our party-loving pagination.

    return $default;
}

And there you have it! With just a smidge of code, your pagination is now the life of the dance floor. Who knew customizing pagination could be such a blast? ๐ŸŽ‰๐Ÿ’ƒ๐Ÿ•บ Letโ€™s keep this party going and check out some other cool Laravel features! ๐Ÿ’ช๐Ÿ˜Ž

Funny Attributes: Because One Size Doesnโ€™t Fit All!

In the wild world of web development, there are times when you might want to serve up a secret sauce (not literally, we hope) only to those who deserve it - say, our esteemed administrators. And thatโ€™s where Laravel steps in with its conditional attribute superpowers!

Letโ€™s imagine a scenario: Youโ€™re hosting a virtual bingo night and you want to send the winning numbers only to your VIP members (who just so happen to be admins). Hereโ€™s how you can use the when method to pull off this clandestine operation:

/**
 * Transform the resource into a fun-filled bingo card.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'bingo_numbers' => [1, 2, 3, 4, 5], // Initial numbers for all users
        'winning_numbers' => $this->when($request->user()->isAdmin(), ['secret-value']), // Only reveal the secret numbers to admins!
    ];
}

Here, our admin friends will be pleasantly surprised with a sneaky peek at the winning numbers, while everyone else gets to enjoy their regular bingo game. The when method makes it possible for you to keep your resources well-balanced and secretive without diving into the conditional chaos of if statements!

But wait, thereโ€™s more! The when method also accepts a clever little function as its second argument, so you can calculate those winning numbers on the fly if the admin condition is met:

'winning_numbers' => $this->when($request->user()->isAdmin(), function () {
    return ['secret-value']; // The magic happens here!
}),

Now, you can impress your users with your mastery of Laravelโ€™s conditional attribute wizardry. But what if you want to include an attribute only when it actually exists? Well, thereโ€™s a method for that too - whenHas:

'user_name' => $this->whenHas('user'), // Only reveal the user name if there is a user associated with this resource

Finally, if you ever find yourself in a situation where you have to handle an empty string as a null value, the whenNotNull method is your knight in shining armor:

'user_name' => $this->whenNotNull($this->user_name), // Only include the user name if it has a non-empty value

So, whether youโ€™re hosting a virtual bingo night or serving up some top-secret data, Laravelโ€™s conditional attribute methods have got your back!

Alright, letโ€™s lighten up this Laravel doc a bit! ๐ŸŽ‰

Slamminโ€™ Together Conditional Attributes ๐ŸŽต

Ever find yourself in a pickle with multiple attributes needing to dance the same conditionally choreographed jive in your resource response? Fear not, my friend! You can now use the mergeWhen method as your dance partner ๐Ÿ•บ๐Ÿผ, making those attributes sashay onto the scene only when the specified condition is a resounding โ€œyesโ€!

/**
 * Transform this resource into a groovy disco ball array.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        // Invite the secret squirrels to the party! ๐Ÿฟ๏ธ๐Ÿž
        $this->mergeWhen($request->user()->isAdmin(), [
            'first-secret' => 'value',
            'second-secret' => 'value',
        ]),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

Remember, if the condition turns out to be a โ€œno way Jose,โ€ these attributes will do the chicken dance offstage before being served up to the client ๐Ÿญ.

Attention

The mergeWhen method is a finicky dance partner and should steer clear of arrays that blend string and numeric keys like a peanut butter and jelly smoothie ๐Ÿฅœ๐ŸŒ. Additionally, itโ€™s got two left feet when paired with out-of-order numeric keys in those arrays - better stick to the cha-cha!

Now, letโ€™s get our boogie on and dance our way through some conditional relationships! ๐Ÿ’ƒ๐Ÿผ๐Ÿ•บ๐Ÿผ

The Art of Conditional Cohabitation! ๐Ÿ ๐Ÿฑโ€๐Ÿ’ป

Ever found yourself in a situation where youโ€™re sharing your data with another model, but only on a โ€˜friends with benefitsโ€™ basis? Meet the Conditional Relationships! Theyโ€™re like that chill roommate who only shows up when invited. ๐ŸŽ‰

Besides loading attributes on demand, now you can include relationships in your responses, depending on whether theyโ€™ve already moved in to the model apartment. This gives your controller the power to decide which tenants are welcome and your resource can easily accommodate them - only if theyโ€™ve actually shown up! et

The whenLoaded method serves as the doorman, letting relationships in based on their RSVP. To prevent overcrowding, it accepts the relationship name instead of the actual tenant:

use App\Http\Resources\PostResource;

/**
 * Transform the resource into a roommate list.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'posts' => PostResource::collection($this->whenLoaded('posts')), ๐Ÿ‘‹๐Ÿผ Welcome the roommates who RSVPed!
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

In this scenario, if no relationship has shown up, the posts key will be politely asked to leave before the party starts! ๐Ÿšช๐Ÿ ๐Ÿ‘‹๐Ÿผ

Alright, buckle up, folks! Weโ€™re about to dive into the whimsical world of Laravelโ€™s Conditional Relationship Counts! ๐ŸŽฉ๐ŸŽน

First off, letโ€™s chat about relationship counts. Ever been in a situation where you wanted to know how many posts a user has, but only if theyโ€™ve been loaded? Well, my friend, youโ€™re in luck! Just whip up a UserResource with a freshly baked loadCount('posts') and voila! ๐Ÿฅ

But wait, thereโ€™s more! Ever wanted to avoid including an attribute if the relationshipsโ€™ count isnโ€™t present? Fear no more! The whenCounted method is here to save the day. Itโ€™s like having a magic wand that eliminates unwanted relationship counts. ๐Ÿง™โ€โ™‚๏ธ

Hereโ€™s an example:

public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'posts_count' => $this->whenCounted('posts'), // Only included if posts count has been loaded! ๐ŸŽˆ
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

Now, letโ€™s talk about aggregates. You know, averages, sums, minimums, and maximums. With Laravel, you can conditionally load these using the whenAggregated method. Itโ€™s like having your very own statistical sidekick! ๐Ÿ“Š

'words_avg' => $this->whenAggregated('posts', 'words', 'avg'),
'words_sum' => $this->whenAggregated('posts', 'words', 'sum'),
'words_min' => $this->whenAggregated('posts', 'words', 'min'),
'words_max' => $this->whenAggregated('posts', 'words', 'max'),

And remember, if the posts relationshipโ€™s count hasnโ€™t been loaded, the posts_count key will be as elusive as a unicorn on roller-skates! ๐Ÿฆ„๐Ÿ›ซ

Happy coding, and may your relationships be ever in your favor! ๐Ÿค˜

Alrighty, letโ€™s dive into the world of Laravelโ€™s Conditional Pivot Information - a dance floor for data enthusiasts and tech wizards alike!

Instead of blindly including relationship info in your resource responses, we offer you a chance to be picky with the data from those glorious many-to-many relationship intermediate tables. How? By using the whenPivotLoaded method, a magic wand that transforms your models into data selectors!

/**
 * Transform this model into a tasty array of data.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'expiration date' => $this->whenPivotLoaded('role_user', function () {
            return $this->pivot->expires_at;
        }),
    ];
}

If your relationship is using a custom intermediate table model, pass it as the first argument to whenPivotLoaded. Itโ€™s like inviting an extra guest to the dance:

'expiration date' => $this->whenPivotLoaded(new Membership, function () {
    return $this->pivot->expires_at;
}),

But what if your intermediate table has a funky name other than pivot? Fret not! Use the whenPivotLoadedAs method to specify the alias:

/**
 * Transform this model into a tasty array of data.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'expiration date' => $this->whenPivotLoadedAs('subscription', 'role_user', function () {
            return $this->subscription->expires_at;
        }),
    ];
}

Remember, with great power comes great responsibility. Use whenPivotLoaded and whenPivotLoadedAs wisely to ensure your data dance is a harmonious one! ๐Ÿ’ƒ ๐Ÿ•บ๏ธ

Unleashing the Superpower of Meta Data! ๐Ÿš€

In the realm of JSON APIs, itโ€™s not just about the resources, but also about their superhero sidekicks โ€“ the metadata! You know, stuff like secret identity links (to the resource or its buddies), and top-secret data about the resource itself. To share this classified information, you simply summon your inner Jedi and conjure it in your toArray method. For instance, when transforming a resource collection:

/**
 * Transform the resource into an array of secrets ๐Ÿคซ.
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'secrets' => $this->collection,
        'links' => [
            'hq' => 'link-value', // Mission Control, obviously!
        ],
    ];
}

When dishing out top-secret metadata from your resources, fear not about clashing with the meta data auto-generated by Laravel when serving up paginated responses. Any additional links you stash away will be merged with the links provided by the paginatorโ€™s Batcave, ensuring a smooth and harmonious exchange of information. ๐Ÿฆ‡๐Ÿš€

Now go forth and unleash your metadata powers! Just remember to keep it hush-hush โ€“ we wouldnโ€™t want the bad guys catching wind of our secrets, would we? ๐Ÿคซ๐Ÿ’ชโœจ

Alright, buckle up, coding cowboys and codebabes! Letโ€™s dive into the thrilling world of top-level meta data. Ever found yourself yearning to include some swanky metadata with a resource response, but only when itโ€™s the main event on the dance floor? Well, grab your lasso and letโ€™s round up that metadata with style!

First things first, saddle up and ride over to your resource class. There youโ€™ll find the with method โ€“ itโ€™s like the cowboyโ€™s trusty steed, always ready for action. To define our meta data, this loyal companion needs to return an array of metadata that should only accompany the resource response when itโ€™s the grand marshal of transformations.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    // We're not just a collection of users, we're a herd of stars!

    /**
     * Corral the resource collection into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        // We wouldn't want to step on anyone's toes in this dance, would we? Let's stick with the original array here.
        return parent::toArray($request);
    }

    /**
     * Get some fancy footwork for our metadata.
     *
     * @return array<string, mixed>
     */
    public function with(Request $request): array
    {
        // Time to put on a show! Let's add some flair to our meta data.
        return [
            'meta' => [
                'showtime' => 'Yeehaw!'
            ],
        ];
    }
}

And there you have it, cowgirls and cowboys! Now when your resource is the sheriff of the response showdown, itโ€™ll strut its stuff with some shiny new metadata. Happy coding, yโ€™all! ๐Ÿค ๐Ÿš€

Alrighty, letโ€™s get this party started! In the world of Laravel, we donโ€™t just create resources like your average Joe; no, sirree! We jazz them up with some tasty meta data.

Think of it as giving your resource a snazzy new hairdo or a funky pair of sunglasses. But instead of fashion accessories, weโ€™re talking about top-shelf data. The secret ingredient? The additional method, available on all resources and just waiting to be unleashed!

To make your resource sashay down the catwalk with a killer outfit, you simply need to call upon this mystical power:

return User::all()
    ->load('roles') // We're not just fetching users here; we're also giving them a fabulous entourage!
    ->toResourceCollection() // Convert your data into a high-fashion couture collection
    ->additional(['meta' => [ // Time to accessorize!
        'key' => 'value',
    ]]);

Now, isnโ€™t that a refreshing change from the usual programming jargon? So go ahead, deck out your resources with some swag and watch them shine brighter than a Kardashian on the red carpet!

Ahoy there! Sailors of the digital seas! ฯ€ยŸโ‚ฌย“ฯ€ยŸยยฝฮฒย€ยฮฒย™ย‚ฮฟฮˆย Laravel, your trusty shipmate, has brought aboard a swashbuckling new addition: the JsonApiResource! ฯ€ยŸยฮ†ฯ€ยŸย›ยณ

This resource class will ensure your responses sail smoothly through the JSON:API specificationโ€™s high seas. Itโ€™s like a friendly pirate who helps organize your treasure (data), map relationships, and even mark the parrots that belong together in sparse fieldsets! ฯ€ยŸยยฃฮฒยœยจ

But wait, thereโ€™s more! Just as a seasoned sailor can set their sails to different winds, JsonApiResource sets the Content-Type header to application/vnd.api+json. No need for navigation charts here - weโ€™ve got your back! ฯ€ยŸยฮŠฯ€ยŸย›ฮˆ

Note

Aye, Laravelโ€™s JSON:API resources are so efficient at managing your treasure that they can even pass the parchment (query parameters) of incoming JSON:API queries, such as filters and sorts. But if you find yourself in need of a navigator to help decipher these charts, Laravelcompanyโ€™s Laravel Query Builder is the perfect compass! ฯ€ยŸโ‚ฌย”ฮฒย˜ย

Now, letโ€™s hoist the anchor and set sail for creating JsonApiResources! ฯ€ยŸยŽยฒฯ€ยŸย›ฮ…ฮฟฮˆย

Unleashing JSON:API Resources, the Hipster Way! ๐ŸŒฎ๐Ÿ’ป

Ready to rock some ultra-trendy data exchange? Itโ€™s time to summon the power of JSON:API resources with a sprinkle of magic Laravel command:

php artisan make:resource PostResource --json-api

This spellcasting will conjure up a snazzy class that extends Illuminate\Http\Resources\JsonApi\JsonApiResource, with hipster properties $attributes and $relationships for your divine customization:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\JsonApi\JsonApiResource;

class PostResource extends JsonApiResource
{
    /**
     * The resource's attributes, man.
     */
    public $attributes = [
        // ...
    ];

    /**
     * The resource's relationships, dude.
     */
    public $relationships = [
        // ...
    ];
}

JSON:API resources are perfect for dishing out data in a super cool format:

use App\Http\Resources\PostResource;
use App\Models\Post;

Route::get('/api/posts/{post}', function (Post $post) {
    return new PostResource($post); // Or channel the power of 'toResource()'! ๐Ÿ”ฎ
});

You can also opt for the convenience method, because who doesnโ€™t love a little shortcut? ๐Ÿš€

Route::get('/api/posts/{post}', function (Post $post) {
    return $post->toResource();
});

This will serve up a JSON:API-compliant dish, just like your grandmaโ€™s organic, fair-trade, gluten-free vegan quinoa bowl:

{
    "data": {
        "id": "1",
        "type": "posts",
        "attributes": {
            "title": "Hello World",
            "body": "This is my first post."
        }
    }
}

To serve up a whole plate of JSON:API resources, use the collection method or the toResourceCollection convenience method (we all love a good convenience store!):

return PostResource::collection(Post::all());

return Post::all()->toResourceCollection();

Unleashing Your JSON:API Treasure Chest!

Ahoy there, matey! Here be two swashbuckling ways to decide which booty (er, attributes) your JSON:API resource shall bear.

First off, the simple seafaring approach: define an $arr_of_loot on yer resource, listing ye olde treasure names. These will magically appear from the model beneath yonder deck:

public $arr_of_loot = ['Title', 'Pirate_Message', 'Shiver_Me_Timestamps'];

Or, if ye be seekinโ€™ full command over yer resourceโ€™s loot, ye can override the toLoot() method on the resource:

/**
 * Get the treasure chest of this pirate ship.
 *
 * @return array<string, mixed>
 */
public function toLoot(Walk_the_Plank $request): array
{
    return [
        'Title' => $this->title,
        'Pirate_Message' => $this->message,
        'Ship_Status' => ($this->parrotIsDead) ? "Dead" : "Alive",
        'Shiver_Me_Timestamps' => [
            'Started_Pillaging' => $this->startTime,
            'Last_Update' => $this->lastUpdated,
        ],
    ];
}

Arrr matey, happy hoarding! ๐Ÿดโ€โ˜ ๏ธ๐Ÿค‘

Ahoy there, captain! Sail into the realm of Laravelโ€™s majestic JSON:API, where relationships are as epic as a dragonโ€™s hoard!

Ye can define relationships that would make a medieval map look like childโ€™s play, all in line with the swashbuckling JSON:API specification. But wait, what be these relationships ye speak of? Theyโ€™re connections between resources, like tying together a shipโ€™s rigging for smooth sailing - only in code!

Now, donโ€™t go asking for โ€˜em willy-nilly, for relationships will only appear when the client requests them via the include query parameter. If you donโ€™t include them, theyโ€™ll be as shy as a timid mermaid hiding underwater.

And remember, just like your trusty sidekick, these relationships are there to support ye on your coding journey. So, hoist the sails and set course for the treasure trove of well-connected resources!

Alright, letโ€™s dive into the $relationships property - your wingman in the world of Laravel resources! This magical property is where you declare your resourceโ€™s lovable relationships that can be embraced (included) when needed. ๐Ÿ’˜

Hereโ€™s how it works:

public $relationships = [
    'author', // Yes, we're getting serious about dating the author!
    'comments', // Waiting for those heartwarming messages from the comments section.
];

When you list a relationship name, Laravel swoops in and resolves the corresponding Eloquent relationship (the fancy date), then discovers the appropriate resource class (our perfect match). ๐ŸŽต โ€œLove is a many-splendored thingโ€ ๐ŸŽถ

But what if youโ€™re a picky dater, wanting to choose your own resource class? No worries, just define your relationships as key/class pairs:

use App\Http\Resources\UserResource;

public $relationships = [
    'author' => UserResource::class, // Swipe right on UserResource! โค๏ธ
    'comments', // Let's keep our options open for comments.
];

Alternatively, you can override the toRelationships method on your resource:

/**
 * Get the resource's relationships (our dating profile).
 */
public function toRelationships(Request $request): array
{
    return [
        'author' => UserResource::class, // Time to present our best self!
        'comments', // Let's not forget about the comments section.
    ];
}

Happy dating (or resource-relating)! ๐ŸŽ‰๐Ÿ’ฌ

Alright, buckle up, folks! Weโ€™re about to embark on a rollercoaster ride through the wilds of Laravel land, where data relationships are as tangled as the roots of a mighty oak tree. But fear not, for this doc is here to guide you through this jungle with a bit of humor and wit. ๐ŸŒฒ

First off, letโ€™s talk about when clients ask nicely for related resources. They can do so using the include query parameter, which, in essence, is like asking โ€œCan I have seconds, please? With a side of author and comments, if you donโ€™t mind?โ€:

GET /api/posts/1?include=author,comments

In return, our server will serve up a multi-course meal, complete with appetizers (the relationships key), main course (top-level data), and dessert (the included array). Hereโ€™s what that might look like on the menu:

{
    "data": {
        "id": "1",
        "type": "posts",
        "title": "Hello World" // Appetizer: title ๐Ÿฅ—
    },
    "relationships": {
        "author": {
            "data": {
                "id": "1",
                "type": "users" // Main course: author ๐Ÿ—
            }
        },
        "comments": {
            "data": [
                {
                    "id": "1",
                    "type": "comments" // Dessert: comments ๐Ÿฐ
                }
            ]
        }
    },
    "included": [
        { // Bonus desserts!
            "id": "1",
            "type": "users",
            "name": "Taylor Otwell" // Author's name
        },
        {
            "id": "1",
            "type": "comments",
            "body": "Great post!" // Comment body
        }
    ]
}

But what if the relationship gets a little moreโ€ฆ incestuous? Letโ€™s say you want to include nested relationships, like commentsโ€™ authors. In that case, simply use dot notation to navigate the family tree:

GET /api/posts/1?include=comments.author

This query is like asking โ€œCan I have seconds, please? With a side of commentsโ€™ author?โ€. And just like that, youโ€™ve unlocked the secret to a delicious dish of nested relationships. ๐Ÿด๐Ÿ˜‹

Now that youโ€™ve got the hang of it, go forth and serve up your own multi-course meals of related data with ease! Just remember, with great power comes great responsibility (and a full stomach). ๐Ÿ’ช๐Ÿฅ‚

Alright, letโ€™s dive into the wild world of Laravel relationships, where data is as connected as your great aunt Mabelโ€™s spaghetti dinner party! ๐Ÿ

By the default settings, our nested relationship dance floor has a strict guest list limit โ€“ only three levels deep. But who wants a boring party? ๐ŸŽ‰ You can kick it up a notch and expand that guest list using the maxRelationshipDepth method, typically found hanging out in one of your applicationโ€™s swanky service provider joints:

use Illuminate\Http\Resources\JsonApi\JsonApiResource;

// It's like saying "more guests please" at a party... but for data!
JsonApiResource::maxRelationshipDepth(3);

Just remember, as with any good party, moderation is key โ€“ donโ€™t go too deep or you might end up like Uncle Bob in the karaoke corner on repeat. ๐ŸŽค๐Ÿ’ฉ

Type Party and Unique Identifier (UID)

In our vibrant, dancefloor of data, each resource gets a groovy name, and a unique ticket to the event. By default, the Type Party is determined by the resourceโ€™s class name disco-ball reflections (yes, really!). So if youโ€™re boogying with PostResource, youโ€™ll be known as posts. If itโ€™s BlogPostResource, well then, letโ€™s get this party started at blog-posts!

As for the Unique Identifier (UID), our bouncer checks this out from the modelโ€™s primary key. But what if you want to change these aliases for a special dance? Just like a stylish suit change, you can override the toType and toId methods on your resource:

/**
 * Get your resource's dance floor nickname.
 */
public function toType(Request $request): string
{
    return 'articles'; // No more posts or blog-posts, we're serving articles here!
}

/**
 * Get your resource's UID (Unique Identifier).
 */
public function toId(Request $request): string
{
    return (string) $this->uuid; // Still the cool kid with a unique ID tag.
}

Now, when youโ€™re wrapping an AuthorResource around a User model and want to spice things up by outputting authors, itโ€™s as easy as a spin on the dance floor! ๐Ÿ’ƒ๐Ÿผโœจ๐Ÿš€

The Art of Selective Resource Snacking ๐ŸŽ‰๐Ÿ”๐Ÿฅจ

In the wild world of JSON:API, we offer an exclusive dining experience - Selective Fieldset Feasts! Clients can now savor only their favorite attributes for each dish (erโ€ฆ resource type), all thanks to the fields query parameter sprinkled in:

GET /api/posts?fields[posts]=title,created_at&fields[users]=name

This delectable order means youโ€™ll only get a taste of the title and created_at attributes for your main course (posts), and a side helping of just the name attribute for your dessert (users). Bon appรฉtit! ๐Ÿ˜‹๐Ÿฅณ

Note: Donโ€™t let the query string fool you, itโ€™s still a part of our secret sauce! ๐Ÿ˜‰

Alrighty, letโ€™s dive into the world of Laravel where we donโ€™t always need a map to navigate those tricky query strings! ๐Ÿ—บ๏ธ๐Ÿ”

If you ever find yourself in a situation where you want to disregard that pesky fieldset filtering for your favorite resource, fear not! You can summon the mighty ignoreFieldsAndIncludesInQueryString method like a Jedi using the Force (or PHP)! ๐Ÿค“

Return of the post!
It's toResource() time, don't you know?
But wait, we need some magic -
Call `ignoreFieldsAndIncludesInQueryString()`, and watch as that pesky filtering disappears like a magician's trick! ๐ŸŽฉ

So there you have it, Laravel wizards! Use this powerful spell to bypass those annoying query strings and let your resources roam free (and unfiltered)! ๐Ÿ˜„๐Ÿ™Œ๏ธ

Ah, the delightful world of Laravel relationships! Where data isnโ€™t just a one-night stand, but rather a deeply committed family. By default, these relationships only hook up when explicitly invited via the include query parameter, much like a shy wallflower at a high school dance. But fear not, for thereโ€™s always that one friend who knows how to break the ice!

If youโ€™re feeling particularly sociable and want all your previously eager-loaded relationships to join the party regardless of the query string, you can call upon the includePreviouslyLoadedRelationships method:

return $post->load('author', 'comments')
    ->toResource()
    ->getWildAndCrazy(); // Alright, maybe not this wild and crazy, but you get the idea!

Now your post doesnโ€™t have to play hard-to-get with its friends - author and comments are always welcome at the dance! ๐ŸŽ‰๐Ÿ’ƒ๐Ÿ•บ๏ธ

Alright, hereโ€™s a light-hearted take on the Laravel JSON:API documentation for those who prefer their code with a side of chuckles!

Links and Meta, A Love Story

Who needs roses when you can code your way to affection? Thatโ€™s right, weโ€™re talking about adding links and meta info to your resources in the JSON:API world!

To get this party started, override the toLinks and toMeta methods on your resource class:

/**
 * Get the resource's links.
 */
public function toLinks(Request $request): array
{
    return [
        'self' => route('api.posts.show', $this->resource), // It's like a secret invitation to your post's page!
    ];
}

/**
 * Get the resource's meta information.
 */
public function toMeta(Request $request): array
{
    return [
        'readable_created_at' => $this->created_at->diffForHumans(), // It's like a time capsule, but for your data!
    ];
}

Now, when you serve up your resource object in the response:

{
    "data": {
        "id": "1",
        "type": "posts", // Don't be shy, introduce yourself!
        "attributes": {
            "title": "Hello World" // Your life story in a nutshell!
        },
        "links": { // A little directory for your post's page
            "self": "https://example.com/api/posts/1"
        },
        "meta": { // Some fun facts about your data
            "readable_created_at": "2 hours ago"
        }
    }
}

And there you have it! A dance between links and meta that will leave your API responses feeling more connected than ever. Just remember, in the world of programming, love is just a well-crafted method away! ๐Ÿ’Œ๐Ÿ’ป๐Ÿ’–

Alrighty then! You already know that resources can be served straight from your routes and controllers like a well-rehearsed stand-up act.

use App\Models\User;

Route::get('/user/{id}', function (string $id) {
    return User::findOrFail($id)->toResource(); // "And now, here's your user!"
});

But sometimes, you might need to tweak the outgoing HTTP response like a DJ mixing tracks. There are two ways to pull this off:

  1. You can chain the response method onto the resource, which will dish out an Illuminate\Http\JsonResponse and hand you the mic for adjusting headers:
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/user', function () {
    return User::find(1)
        ->toResource() // "Step right up, folks! Here's your user!"
        ->response() // "Now let me adjust the EQ and add a little reverb..."
        ->header('X-Value', 'True'); // "...and there we have it!"
});
  1. Alternatively, you can define a withResponse method within the resource itself. This method gets called when the resource is the main event in the response:
<?php

namespace App\Http\Resources;

// ...

class UserResource extends JsonResource
{
    // ...

    /**
     * Customize the outgoing response for the resource.
     */
    public function withResponse(Request $request, JsonResponse $response): void
    {
        $response->header('X-Value', 'True'); // "And while I've got you here, let me add a special header!"
    }
}

So there you have it! Now you can serve up resources like a pro comedian, and customize the response to make your audience laugh (or at least not boo). Happy coding! ๐Ÿ˜„๐Ÿš€๐ŸŽค๐ŸŽญ

Our network