--level used: defaultWithin the User model I have the following relationship
public function coupons()
{
return $this->belongsToMany(Coupon::class);
}
Which returns the following error when analysed
Access to an undefined property App\Models\User::$coupons.
Such error is caused by the following line within the _UserController_:
$user->coupons->count();
Hi,
You need to define the return type of the relation method. Either in the docblocks
/**
* @return BelongsToMany
*/
public function coupons()
{
return $this->belongsToMany(Coupon::class);
}
or as the native return type
public function coupons(): BelongsToMany
{
return $this->belongsToMany(Coupon::class);
}
then Larastan will understand the relation and property access.
None of the methods described work
@grcasanova Are you getting the same error? Can you share the updated code snippet to reproduce it?
Ok solved. Two notes for reference.
HasOne, BelongsTo, HasMany, etcuse Illuminate\Database\Eloquent\Relations\HasOne;
Most helpful comment
Hi,
You need to define the return type of the relation method. Either in the docblocks
or as the native return type
then Larastan will understand the relation and property access.