Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brianroyer's avatar

@elslay Your use of local query scopes is not properly implemented. See here: Laravel - Local Query Scopes

The query scopes belong in a model.

<?php

// User model:
public function scopeNonMute($query)
{
    return $query->whereRaw('(((`date_controle` is null AND (date_mutation < DATE_SUB( curdate() , INTERVAL 6 MONTH))) or (date_prev_controle < curdate()) or (date_controle is null and date_mutation is null)) AND (date_depart IS NULL OR curdate() < date_depart ))');
}

public function scopeListFormateur($query)
{
    return $query->whereHas('cfds', function($q) {
            $q->where('isEM', 1);
        })
        ->with('grades')
        ->with('cfds')
        ->where('isFormateur', true)
        ->where('isChefdeCours', false)
        ->where('isExterieur', false);
}

// Controller or Model:
public static function getRetardFondDeSalleEM()
{
    return User::listFormateur()->nonMute()->orderBy('created_at')->get();
}

Something like this should give you an idea how eloquent interacts with query scopes and how the flow works in Eloquent.

Previous

Please or to participate in this conversation.