mardi 20 juin 2017

Laravel 5.1 Eager Loading Cannot do additional where

I don't know what i'm doing wrong here but this code

$models = Model::with(['relationship' => function ($query) {
        $query->whereBetween('date', [$this->today->copy()->startOfDay(), $this->today->copy()->endOfDay()]);
    }])->where('status', 'active')->where('position', '!=', 'dev')->where('status', '=', 'good')->get();

doesn't work as expected. It does eager load the relationship it exists in the object but the problem is I cant do something like this

foreach($models as $model)
    {
        echo $model->relationship->where('status', '=', 'somestatus')->count() . '<br>';
    }

it returns 0 but when I check in database it is not 0. Do you guys know why does this code not work? Thank you guys.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire