Home> PHP Framework> Laravel> body text

How to exclude unwanted fields in Laravel model query

PHPz
Release: 2023-04-23 09:44:15
Original
1548 people have browsed it

Laravel is a very popular PHP web development framework that provides powerful and flexible database operation functions. When using Laravel to query data, we often need to filter and return certain specified fields, but in some cases, we need to exclude certain fields so that they do not appear in the query results. This article explains how to exclude unwanted fields in Laravel model queries.

First, we can use theselect()method provided by Laravel to specify the fields of the query, for example:

$users = User::select('name', 'email')->get();
Copy after login

This will return a ## containing each user A collection of #nameandemailfields. But what if we need to exclude some fields? The following are two ways:

Method 1: Exclude fields

We can use the

select()method to specify all fields to be returned, and then useexcept ()method to exclude unnecessary fields. For example:

$users = User::select('id', 'name', 'email', 'password') ->get() ->map(function ($user) { return collect($user->toArray()) ->except(['password']) ->all(); });
Copy after login
Here we first use the

select()method to specify all the fields to be returned, and then use theget()method to execute the query. Then we use themap()method to process the query results, convert each user's information into an associative array, and use theexcept()method to exclude its password field .

Method 2: Hidden fields

Laravel also provides a simpler method, which is to use the

$hiddenattribute of the model to hide fields that do not need to be output. For example:

class User extends Model { protected $hidden = ['password']; }
Copy after login
In this example, we set the

$hiddenattribute of the user model to['password'], so that when querying, Laravel will automatically Exclude password fields from results.

It should be noted that if we need to output a hidden field, we can use the

makeVisible()method to override the$hiddenattribute of the model when querying . For example:

$user = User::find(1); $user->makeVisible(['password']);
Copy after login
This will cause the obtained

$userobject to contain a password field.

In short, the above two methods can help us exclude unwanted fields in Laravel model queries. When using it, we can choose which method to use according to the actual situation.

The above is the detailed content of How to exclude unwanted fields in Laravel model query. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!