select in laravel between tables many to many

0

To select specific columns in laravel by select by obligation you must bring the id and the fk so that the tuples can be related, but when I want to do a - > select () between a much relation a lot does not work for me since in the middle there is a pivot table and I guess that you should still load your pk and fk. as is usually done in relations of other cardinalities.

How should I put the fk and pk of the pivot table in the select to bring specific fields?

Suppose we have a notification that is related to many-to-many users, and that notification may have N comments related to a notification (a notification may have N comments, a comment may only be in a notification)

for example

 $comentarios = Comentario::select('id','texto','users_fk','notificaciones_fk','created_at','updated_at')
                             ->where('notificaciones_fk', $request->notificacion_id)
                             ->where('activo', 1)
                             ->orderBy('id','DESC')
                             ->with(['notificacion.users' => function($query){
                                $query->with('perfil');
// En este $query aplicar un select para traer campos específicos de usuarios, 
// pero al haber una tabla pivote de por medio, como debo especificar la id y las fk 
// de la tabla pivote? Independientemente de que utilice select() o 
// cargue con eager loading los atributos :id,atributo,atributo_fk debo
// especificar los primary y foreign para que se pueda llevar a cabo la
// consulta

                             }])
                             ->paginate($perPage);

Or failing that, is there a way to remove or hide columns from queries other than adding fields in the $ hidden property of the model?

    
asked by César Alejandro M 08.07.2018 в 19:44
source

1 answer

0

The pivot table can be obviated from defining its pk and fk, but it is necessary in tables that are not pivot when making the selection to previously define the name of the table.

nombredelatabla.propiedad
    
answered by 12.07.2018 в 17:21