Category: Laravel, PHP

If you enjoy reading my blog, you could consider supporting me on Patreon. An abstract resource class will inherit the base Resource class.

We start by creating a file AbstractResource.php inside app/Nova, like this: At first, the AbstractResource looks like this: Next, in your Resource classes just inherit from this abstract Resource instead of the Nova Resource one: So, let's look at some examples of improvements you can add to your new abstract resource.

As an example, let's say you want to add a new Card to all of the model resources that share your abstract resource, you can do it like: and in your model Resource: The BelongsTo field already has an option to remove the checkbox 'With Trashed' (basically not to show trashed items), but what if want to remove it from any other relationship operation (e.g.: BelongsToMany)? You just need to apply the following code in your abstract resource: in this example, all you have to do is to define a policy for your model, and then create a new method called trashedAny(User $user), as example: These were examples that can trigger your thoughts about how to leverage Abstract Resources on your Nova projects.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners