Avoid loading certain fields of an eloquent query

1

I am making a query in this way. To load all sales with their respective relationships, It turns out that everything is fine.

	$ventas = Venta::with(['categoria','user','foto'])
					->orderBy('id','DESC')
					->where('status','PUBLICADO')
					->paginate(10);

but I would like certain fields eg. of user will not be charged as good. When he sent you to a hearing. I only show what is necessary, but how can I prevent it from loading? the "user password" field?

I made a dd

#items: Collection {#546 ▼
#items: array:5 [▶]

And this shows me.

Each sale has its data. And all perfect. but . .

0 => Venta {#452 ▼
        #connection: "mysql"
        #table: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:9 [▶]
        #original: array:9 [▶]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: array:3 [▶]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▶]
      }

these are your relationships.

   #relations: array:3 [▼
          "categoria" => Categoria {#487 ▶}
          "user" => User {#547 ▶}
          "foto" => Foto {#645 ▶}
        ]

but I'm loading the password field :(

"user" => User {#547 ▼
            #fillable: array:3 [▶]
            #hidden: array:2 [▶]
            #connection: "mysql"
            #table: null
            #primaryKey: "id"
            #keyType: "int"
            +incrementing: true
            #with: []
            #withCount: []
            #perPage: 15
            +exists: true
            +wasRecentlyCreated: false
            #attributes: array:12 [▼
              "id" => 3
              "nick" => "Taniaħ"
              "email" => "[email protected]"
              "password" => "$2y$10$BaEUQJdHCmZsqIuf0VGbkOdJmYi32kPx/Ce6Azyu.o.Xajtg9DCSS"
              "nombre" => "Tani"
              "apellidoP" => null
              "apellidoM" => null
              "fotoPerfil" => "http://carrefourjeunesse.fr/wp-content/uploads/2017/06/female1-512.png"
              "fechaNacimiento" => null
              "remember_token" => "QSKRWXcCejTA4Px0kWMhmrK3BMgxiSiAKux7yEp7HDOd8xx9YkUu0YiFtK5X"
              "created_at" => "2018-09-12 15:55:57"
              "updated_at" => "2018-09-12 15:55:57"
            ]
    
asked by Alex Burke Cooper 16.09.2018 в 02:26
source

2 answers

1

There are several ways to do it, the first is to add an array of attributes in your model that you want to hide:

class User extends Model{

   //......


   protected $hidden = ['password'];
} 

The downside of this solution is that you will ALWAYS hide the "password" field when you make a query. For this to work, you must convert your result to an array or JSON with the ->toArray() method or the toJson() method respectively.

The other way is to make a selection specifically of the columns you want to be displayed:

$ventas = Venta::with(['categoria','user' => function($query){
   $query->select(['id', 'nick', 'email', 'nombre', 'apellidoP', 'apellidoM', 'fotoPerfil', 'fechaNacimiento', 'remember_token', 'created_at', 'updated_at']);
},'foto'])
                    ->orderBy('id','DESC')
                    ->where('status','PUBLICADO')
                    ->paginate(10);

Another solution that only applies from laravel 5.6 onwards, specify the columns within the function parameter with :

$ventas = Venta::with(['categoria','user:id,nick,email,nombre,apellidoP,apellidoM,fotoPerfil, fechaNacimiento,remember_token,created_at,updated_at','foto'])
                    ->orderBy('id','DESC')
                    ->where('status','PUBLICADO')
                    ->paginate(10);
    
answered by 17.09.2018 / 20:41
source
1

You can use the following code to do it:

$ventas = Venta::with(['categoria','user' => function($query){
   $query->select(['id', 'nick', 'email', 'nombre', 'apellidoP', 
 'apellidoM', 'fotoPerfil', 'fechaNacimiento', 'remember_token', 
'created_at', 'updated_at']);
},'foto'])
                    ->orderBy('id','DESC')
                    ->where('status','PUBLICADO')
                    ->paginate(10);
    
answered by 17.09.2018 в 21:48