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

0

I'm moving with laravel and I'm learning object orientation and wanted to do a select without using the join but as object oriented and I know I'm not getting it.

As I'm accustomed 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 all sorts of shapes all but unsuccessfully

Some ways I've 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();
    
asked by anonymous 12.07.2018 / 21:12

1 answer

0

Hello

You could use this, but you should take a look at eloquent. link

$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))"));
    
12.07.2018 / 22:48