Skip to content

Extract Eloquent models scopes #226

Discussion options

You must be logged in to vote

Solution 2: Custom Eloquent Builder methods traits

Pros

  • Scopes are encapsulated in specialized Eloquent Builder classes
  • IDE friendly, can find scope method usages
  • Methods named exactly like they are used

Cons

  • Increased entry threshold to start using package, not all developers familiar with custom Eloquent Builders feature
  • Breaking change, will require to add custom Eloquent Builders and use traits in them

Usage

$usersReactedByMe = User::query()->whereReactedBy($myUser)->get();

Implementation

<?php

declare(strict_types=1);

namespace App\Models;

use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
use Cog\Laravel\Love\Reacterable\Models\Traits\Reacterable;
use Illuminate\Database\E…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@vesper8
Comment options

@antonkomarev
Comment options

@antonkomarev
Comment options

@antonkomarev
Comment options

@vesper8
Comment options

Comment options

You must be logged in to vote
1 reply
@antonkomarev
Comment options

Answer selected by antonkomarev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants