Eloquent is a razor-sharp tool that is adored by many. It allows you to carry out database operations with ease while maintaining an easy-to-use API.
Next, create an HTTP middleware whose responsibility is going to be applying the scope to the relevant models: final readonly class RestrictByCountry { public const NAME = 'country.restrict'; public function __construct(private Repository $geo) {} public function handle(Request $request, Closure $next): mixed { $scope = new CountryRestrictionScope($this->geo->get()); Movie::addGlobalScope($scope); Rating::addGlobalScope($scope); Review::addGlobalScope($scope); return $next($request); }}
You'd want to generate and persist a value like CA‑01‑00001 but actually save it in three distinct columns (number_country - number_department - number_sequence) for much easier querying: final readonly class AsProductNumber implements CastsAttributes { public function get( $model, string $key, $value, array $attributes ): string { return implode('-', [ $attributes['number_country'], Str::padLeft($attributes['number_department'], 2, '0'), Str::padLeft($attributes['number_sequence'], 5, '0'), ])} public function set( $model, string $key, $value, array $attributes, ): array {[$country, $dept, $sequence] = explode('-', $value, 2); return [ 'number_country' => $country, 'number_department' => (int) $dept, 'number_sequence' => (int) $sequence, ]; }}
That's when it suddenly struck me to create a read-only model for the media table and put the theory to the test: final class File extends Model implements Sortable { use SortableTrait; public array $sortable = [ 'order_column_name' => 'order_column'; ]; protected $table = 'media'; public function buildSortQuery(): Builder { return $this->newQuery()->where($this->only('model_id')); }}