Route model Binding

I have different models, where I have a published_at date as a field and I have different routes, where I use these models with route bindings. On the front end, I only want to show "published" models (aka published_at > now()). This is easily done by adding the following to the models:
public function resolveRouteBindingQuery($query, $value, $field = null)
{
return parent::resolveRouteBindingQuery($query, $value, $field)->where("published_at", ">", now());
}
public function resolveRouteBindingQuery($query, $value, $field = null)
{
return parent::resolveRouteBindingQuery($query, $value, $field)->where("published_at", ">", now());
}
The problem is, that this logic obviously also kicks in in admin panel, so I get a 404 on "unpublished" resources. Any elegent solutions for this? For example, can I exclude somehow the admin from my resolveRouteBindingQuery logic?
1 Reply
awcodes
awcodes16mo ago
Sounds like a scope would be better. Then you could either add or remove it as necessary.
Want results from more Discord servers?
Add your server