In Laravel, we can convert the model into an array through the toArray()
method. This method is very convenient because we often need to return model data to the client or use it in templates.
However, in actual development, we may encounter some special situations where we need to customize the output format of the array. Let's introduce how to convert the model into an array in Laravel, and how to customize the output format of the array.
toArray()
method is the most commonly used method in Laravel to convert a model into an array. This method will convert the attributes and relationships of the model object into an array, which is very convenient for output to the client.
The following is an example of using the toArray()
method to convert a model into an array:
$user = User::find(1); $array = $user->toArray();
In this example, we use User::find( 1)
Obtains a user object, and then uses the toArray()
method to convert it into an array.
The converted array usually looks like this:
[ 'id' => 1, 'name' => '张三', 'email' => 'zhangsan@example.com', 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00', 'articles' => [ [ 'id' => 1, 'title' => '文章标题', 'content' => '文章内容', 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00', ], ... ], ]
As you can see, the converted array contains the main attributes of the model and the associated relationships. If we do not need the associated relationship, we need to define the $hidden
attribute in the model:
class User extends Model { protected $hidden = ['articles']; }
If we need to customize the model object Conversion method, you can implement the toArray()
method in the model. This method will override the default conversion method to achieve customized output effects.
The following is an example of a custom toArray()
method that shuffles the properties of a model object and only outputs some of the properties:
class User extends Model { protected $hidden = ['articles']; public function toArray() { $array = parent::toArray(); $keys = array_keys($array); shuffle($keys); $newArray = []; foreach ($keys as $key) { if (in_array($key, ['id', 'name', 'email', 'created_at'])) { $newArray[$key] = $array[$key]; } } return $newArray; } }
In this example, We first call the toArray()
method of the parent class to obtain the default conversion result. Then, we use the shuffle()
method to shuffle the keys in the array, and finally only some attributes are output.
The output of this custom toArray()
method may look like this:
[ 'name' => '张三', 'email' => 'zhangsan@example.com', 'id' => 1, 'created_at' => '2021-01-01 00:00:00', ]
In some cases, we Some attributes of the model need to be output in a specific form, such as formatting the date into a specified format, or converting a JSON string into an array.
In Laravel, you can use the $casts
attribute to achieve this purpose. $casts
The attribute is an array, the key is the attribute name of the model, and the value is the format to be converted.
Here is an example of using the $casts
attribute to output a model's date attribute as a Unix timestamp:
class MyModel extends Model { protected $casts = [ 'created_at' => 'timestamp', 'updated_at' => 'timestamp', ]; }
In this example, we will created_at The type of the two attributes
and updated_at
is set to 'timestamp', so that when the model is converted to an array, the values of these two attributes will be converted into Unix timestamps.
In this article, we introduced how to convert the model into an array in Laravel and how to customize the output format of the array. In addition to the toArray()
method, you can also use the custom toArray()
method and $casts
attribute to achieve more flexible array conversion. Being proficient in these methods will allow us to handle model transformation more conveniently during development.
The above is the detailed content of A brief analysis of three methods of converting models into arrays in laravel. For more information, please follow other related articles on the PHP Chinese website!