I have a Laravel model Product
and Vouchers
defined as follows:
class Product extends Model {
public function vouchers(): HasMany {
return $this->hasMany(Voucher::class);
}
}
and
class Voucher extends Model {
public function product(): BelongsTo {
return $this->belongsTo(Product::class);
}
}
The voucher table has a column status
. Values in status can be ‘active’ or ‘inactive’. I want to create a local scope so I can get the first active voucher on a product.
In the Product model, I defined the following local scope:
public function scopeActiveVouchers($query)
{
return $query->vouchers->first()->where('status', 'active');
}
But this does not work. I always get
Property [vouchers] does not exist on the Eloquent builder instance.
How can I change the local scope so that the first active voucher for a given product is returned?
2
Answers
Query Scope is supposed to add additional query parameter to the model itself, not to a related model.
You shouldn’t add a scope in your Product that supposed to add additional parameter on your Voucher model. Otherwise, you have to manually join the voucher table to load it when your Product scope is called.
You can instead just add additional relation in your product model for querying specific Voucher
then you can do something like;
or even just do it on your query directly
If you still want to use scope, you could try
then
I would do something like this. first define a scope.
Then use it like this: