Allow to use Laravel's ApiResources to define a listing #227
Replies: 2 comments 4 replies
-
What's your use case for this? |
Beta Was this translation helpful? Give feedback.
-
Hey! Unfortunately, I've had a think about it and decided against allowing for custom API Resources on the listing table, for the moment at least. Statamic doesn't allow for defining your own resources in Collection Listing Tables and I want to try and mirror Statamic Core as much as I can in that regard & not introduce other complexities. However, I believe there are other ways to achieve what you're after. For example: you could define an accessor on your Eloquent model which would allow you to format a field however you want. // app/Models/Product.php
use Illuminate\Database\Eloquent\Casts\Attribute;
class Product extends Model
{
protected $appends = ['runway_price'];
public function runwayPrice(): Attribute
{
return Attribute::make(
get: fn (string $value) => '$'.number_format($value, 2),
);
}
} In my example above, I've defined a You'd then need to add a field to your resource's blueprint using the same name as the accessor and you're pretty much there! -
handle: runway_price
field:
input_type: text
antlers: false
display: 'Runway Price'
type: text
icon: text
listable: true
instructions_position: above
visibility: hidden If you only want the field to show on the listing table & not on the publish forms (eg. because it's a special, formatted version of the field), then make sure to set the Thanks for using Runway - hope you have a great rest of your day! 🙂 |
Beta Was this translation helpful? Give feedback.
-
Laravel's built-in ApiResources would allow a lot of flexibility when defining how a resource should be listed in the front end.
This could be defined using an 'api_resource' key in the config set to the ApiResource class that the collection should be called on.
For the CP a simple loop over all the keys/values in the Resources' output would suffice.
Beta Was this translation helpful? Give feedback.
All reactions