Leftjoin does not fetch id values - Laravel

Asked

Viewed 54 times

0

I’m using leftJoin to bring data from 2 tables that are linked by a Belongstomany method to my index view. Virtually all data of the two tables appear when I give a dd in my variable, except the $id of my user. Running this code:

public function index(Request $request)
{
    $setores = Setores::get();
    $filiais_user = User::leftJoin('filiais_users','users.id','filiais_users.users_id')
                        ->leftJoin('filiais','filiais.id','filiais_users.filiais_id')
                        ->select('users.*','filiais.id');

    dd($filiais_user);

That is what is returned:

inserir a descrição da imagem aqui

In my User Model, I have the relationship this way:

public function filiais()
{
    return $this->belongsToMany('App\Filial', 'projeto.filiais_users', 'filiais_id','id');
}

I have tried to specify in select so that it selects the column id on the table users, but it keeps returning without the values of this column. Can anyone indicate where is my error or any way that can solve?

1 answer

0


I believe you need ->get() to send Builder

public function index(Request $request)
{
    $setores = Setores::get();
    $filiais_user = User::leftJoin('filiais_users','users.id','filiais_users.users_id')
                        ->leftJoin('filiais','filiais.id','filiais_users.filiais_id')
                        ->select('users.*','filiais.id')->get();

    dd($filiais_user);
  • Even with the ->get(), the id ta returning value Null

  • puts your entire model to take a look

  • I figured it out, I took out the join and used only one with pulling the branch function I already had on the model, I appreciate your help. I will close the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.