Support the ongoing development of Laravel.io →
Requests Input Eloquent

Good night,

In this code, i got an error that says that the variable userId doesn't exists when its called inside the subquery (whereIn).

If i call it before the return line, i got it's value.

public static function despesasFamiliares ( $periodoDe, $periodoAte, $userId ) {

    return $despesas = \DB::table('despesas')
        ->select('despesas.id', 'despesas.descricao', 'despesas.valor', 'despesas.data', 'despesas_categoria.descricao AS categoriaDescricao', 'despesas_origem.descricao AS origemDescricao' , 'users.username AS usersNome')
        ->join('despesas_categoria', 'despesas_categoria.id', '=', 'despesas.despesas_categoria_id')
        ->join('despesas_origem', 'despesas_origem.id', '=', 'despesas.despesas_origem_id')
        ->join('users', 'users.id', '=', 'despesas.users_id')
        ->whereIn('users_id', function($query)
        {
          $query->select('users_id')
                ->from('users_share_despesas')
                ->where('allowed_users_id', '=', $userId);
        })
        ->where('data', '>=', $periodoDe)
        ->where('data', '<=', $periodoAte)
        ->where('despesas.deleted_at', '=', null)
        ->orderBy('data')
        ->get();

}

This could be a bug? Or there is some write error???

Thank you all

Last updated 3 years ago.
0

you have missed the use keyword

 ->whereIn('users_id', function($query) use( $userId )
Last updated 3 years ago.
0

Thanks

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

tecnical tecnical Joined 4 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.