skip to Main Content

A project I’ve taken over creates relationships in a rather odd way that I’ve never seen done before.

consider one such example:

public function invites(): BelongsToMany
{
    return $this->belongsToMany(
        Invite::class,
        'user_invites',
        'id',
        'user_id'
    )->where('is_active', 1);
}

I have concerns for such code as Laravel doesn’t seem to endorse such use and also phpstan raises concerns about it as well.

My question is, should I consider refactoring the code so the relationship isn’t being setup this way or is this fine and I just need to tell phpstan to ignore this.

2

Answers


  1. Putting a where clause in the relationship function, while uncommon, doesn’t seem to be discouraged in Laravel’s documentation:

    public function currentPricing(): HasOne
    {
        return $this->hasOne(Price::class)->ofMany([
            'published_at' => 'max',
            'id' => 'max',
        ], function (Builder $query) {
            $query->where('published_at', '<', now());
        });
    }
    

    https://laravel.com/docs/11.x/eloquent-relationships#advanced-has-one-of-many-relationships

    return $this->belongsToMany(Role::class)
                    ->wherePivot('approved', 1);
     
    return $this->belongsToMany(Role::class)
                    ->wherePivotIn('priority', [1, 2]);
     
    return $this->belongsToMany(Role::class)
                    ->wherePivotNotIn('priority', [1, 2]);
     
    return $this->belongsToMany(Podcast::class)
                    ->as('subscriptions')
                    ->wherePivotBetween('created_at', ['2020-01-01 00:00:00', '2020-12-31 00:00:00']);
     
    return $this->belongsToMany(Podcast::class)
                    ->as('subscriptions')
                    ->wherePivotNotBetween('created_at', ['2020-01-01 00:00:00', '2020-12-31 00:00:00']);
     
    return $this->belongsToMany(Podcast::class)
                    ->as('subscriptions')
                    ->wherePivotNull('expired_at');
     
    return $this->belongsToMany(Podcast::class)
                    ->as('subscriptions')
                    ->wherePivotNotNull('expired_at');
    

    https://laravel.com/docs/11.x/eloquent-relationships#filtering-queries-via-intermediate-table-columns

    Login or Signup to reply.
  2. This is fine

    public function invites(): BelongsToMany
    {
        return $this->belongsToMany(
            Invite::class,
            'user_invites',
            'id',
            'user_id'
        )->where('is_active', 1);
    }
    

    but this is better:

    public function invites(): BelongsToMany
    {
        return $this->belongsToMany(
            Invite::class,
            'user_invites',
            'id',
            'user_id'
        );
    }
    
    public function activeInvites(): BelongsToMany
    {
        return $this->invites()->where('is_active', 1);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search