just make a new relationship and have that one return hasMany(Photo::class)->first().
Eloquent Relation Scopes?
Hi all,
Suppose I have two Eloquent models, one for 'Product' and one for 'Photos'. Now suppose I have a many-to-many relationship between the two (don't ask, silly architecture needs for this particular project).
As it stands, if I want to get the first photo, I need to to access it like $product->photos->first(). However, there are many places where I need to get the first photo, and it feels cumbersome to handle it like this.
Is it possible to define a relationship scope that will always return the first photo? So I could write something like $product->firstPhoto and this would always return the first photo as a single object, rather than an array of objects?
EDIT: Oh, and just for fun, there are additional attributes in the pivot table, so any written relation can't bypass it.
Please or to participate in this conversation.