Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
Null is given when trying to find data from pivot table

I have a Many To Many relationship between User & Wallet Models:

Wallet.php:

public function users()
    {
        return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
    }

User.php:

public function wallets()
    {
        return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
    }

And the pivot table user_wallet goes like this:

enter image description here

Then at the Controller, I need to access the balance field:

public function chargeWallet(Request $request, $wallet, $user)
{ 
            // $wallet is wallet_id (2) & $user is user_id (373)
            $bal = Wallet::with("users")
                ->whereHas('users', function ($query) use ($user) {
                    $query->where('id',$user);
                })->where('id', $wallet)->first();
            dd($bal->balance);
}

But now I get null as the result of dd($bal->balance) !!

So what is wrong here? How can I properly get the balance ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Since it is a Many to Many relationships, you have Users attached to many Wallets, and Wallets attach to many Users. For each relation between a single Wallet and a single User: you have a pivot value (pivot relation). From your query, you'll retrieve Wallet with Users, each user's having the pivot relation value attached. So to retrieve the pivot table data (for each relation) you have to use a loop (added a with callback to make sure that only Users with matching user_id are eager loaded with the Wallets):

$bal = Wallet::with(["users"=>function ($query) use ($user) {
                $query->where('user_id',$user);
            }])
            ->whereHas('users', function ($query) use ($user) {
                $query->where('user_id',$user);
            })->find($wallet);

To avoid repeating same callback (where user_id =) you can assign it to variable:

$callback = function ($query) use ($user) {
     $query->where('user_id',$user);
};

then use it in your query:

 $bal = Wallet::with(['users' => $callback])
                ->whereHas('users', $callback)->find($wallet);

and then use a foreach loop:

foreach ($bal->users as $value){
   dd($value->pivot->balance);
}

or

if you only want to return the pivot value for the first User of the first Wallet of your query, then:

$user = $bal->users->first();
dd($user->pivot->balance);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda