Can you explain differences between eloquent and \ DB :: in Laravel?

0

I just started with Laravel and I have several problems ... the fact is that I was using the \ DB ::

Like this for example that you already know:

$macUsuario=\DB::table($this->tablaMac)
                ->join($this->tablaUsuario, $this->tablaUsuario . '.Correo', '=', $this->tablaMac . '.Id_usuario')
                ->select('Direccion_mac', 'Descripcion', 'Correo', 'Nombre', 'Apellidos', 'Fecha')
                ->where('Departamento', '=', $departamento)
                ->get();

And I've been told to use the OROs of eloquent "or something like that", I've seen this video

link

It turns out that at minute 21.04 it does:

$estudiante = Estudiante::find(1); 

If I understand correctly, you will look for the PrimaryKey "1" in the Student table, no?

The fact is that I see no difference with using

$persona=\DB::table($this->Estudiante)
         ->select("Nombre","Apellido")
         ->where('pk','=',1)
         ->get();

The one to use less line of code, but I see it better because this way you do not bring all the fields of the BBDD, only the ones that you need ...

Also if possible, tell me more videos or documentation for newbies to understand it better, on YouTube you only have the videos of Duilio Palacios and I do not know anything at all.

Thanks for your time.

    
asked by EduBw 11.12.2017 в 20:34
source

1 answer

1

With

$estudiante = Estudiante::select('Nombre','Apellido')->find(1);

Solamante selecionas the columns Name and Surname.

Eloquent returns a collection of objects and their relations (important), so that to obtain, for example, the subjects of a student you will not have to make a join since Eloquent gives it to you:

$clases=Estudiante->clases;

The alternative would be something similar to the code you have set as an example:

$macUsuario=\DB::table($this->tablaMac)
                ->join($this->tablaUsuario, $this->tablaUsuario . '.Correo', '=', $this->tablaMac . '.Id_usuario')
                ->select('Direccion_mac', 'Descripcion', 'Correo', 'Nombre', 'Apellidos', 'Fecha')
                ->where('Departamento', '=', $departamento)
                ->get();

Once you have defined the model and the relationships, you do not have to return to the consultations.

And as a recommendation of videos and / or documentation, my recommendation is that you take a Duilio Palacios course on the one hand and take a look at the Laravel documentation (now in Spanish) Laravel Documentation in Spanish . When I wanted to learn something new from Laravel, they have been my sources.

    
answered by 12.12.2017 / 08:56
source