vendredi 2 mars 2018

Laravel Eloquent where clause on relationship with joining relationship

I'm trying to show all users with their point values between dates. The point values need to between a start date and end date range. The problem I'm having is I have a ActivityName model that joins to the ActivityPoints to show the category name. I've always known to add a where clause within the join for something like this but I'm not sure where the joining relationship will fall.

$SalesReps = User::with('ActivityPoints', function ($query) use ($StartDate, $EndDate) {
                $query->where('created_at', '>=', $StartDate);
                $query->where('created_at', '<=', $EndDate);
            }, 'ActivityPoints.ActivityName')
            ->whereIn('id', $SalesRepsData->pluck('id')->toArray())
            ->get();

This returns the error: mb_strpos() expects parameter 1 to be string, object given

Obviously, I can do something like:

$SalesReps = User::with('ActivityPoints', 'ActivityPoints.ActivityName')
            ->whereHas('ActivityPoints', function ($query) use ($StartDate, $EndDate) {
                $query->where('created_at', '>=', $StartDate);
                $query->where('created_at', '<=', $EndDate);
            })
            ->whereIn('id', $SalesRepsData->pluck('id')->toArray())
            ->get();

but it won't show the users with zero points (null values).

Thanks!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tcabJK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire