Laravel - do DB::table() without using Join

Asked

Viewed 280 times

-1

I’m playing with Aravel and I’m learning object orientation and I wanted to make a select without using the Join as and object oriented and I know little n I’m getting.

as I’m used to using:

$results = DB::select("SELECT u.foto, u.Ninck,o.Idsala,p.Aceitou, u.Id FROM Onsalas o , Usuarios u, Partidas p WHERE (o.Idusuario=u.Id) AND (u.Id != $Id) AND(o.Idsala = $sala) AND ((p.IdDesafiado = $Idop)  OR (p.IdDesafiante = $Idop))  ");

I tried to make ways but all failed

some ways I tried:

 $results =  DB::table(' \'Onsalas\' , \'Usuarios\' ')      
                        ->select('Onsala.Id')->get();

 $results =  DB::table('Onsalas' , 'Usuarios')      
                        ->select('Onsala.Id')->get();



$results =  DB::table('Onsalas')->table( 'Usuarios')      
                        ->select('Onsala.Id')->get();

1 answer

0

Hello

You could use this, yet you should take a look at the eloquent. https://laravel.com/docs/5.6/eloquent

$users = DB::table('Onsalas')
    ->join('Usuarios', 'Onsalas.Idusuario', '=', 'Usuarios.Id')
    ->join('Partidas', 'Partidas.Idusuario', '=', 'Usuarios.Id')
    ->select('Usuarios.foto', 'Usuarios.Ninck', 'Onsalas.Idsala', 'Partidas.Aceitou', 'Usuarios.Id')
    ->where('Usuarios.Id', '=', $Id)
    ->where('Onsalas.Idsala', '=', $sala)
    ->where(function ($query) use ($Idop) {
        $query->where('Partidas.IdDesafiado', '=', $Idop)
            ->orWhere('Partidas.IdDesafiante', '=', $Idop)
    })
    ->get();

Update not using joins:

$resultados = DB::select( DB::raw("SELECT u.foto, u.Ninck,o.Idsala,p.Aceitou, u.Id FROM Onsalas o , Usuarios u, Partidas p WHERE (o.Idusuario=u.Id) AND (u.Id != $Id) AND(o.Idsala = $sala) AND ((p.IdDesafiado = $Idop)  OR (p.IdDesafiante = $Idop))"));
  • ai u used Join was wanting to do without using Join

Browser other questions tagged

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