Laravel 5.1 Document Guide--eloquent: Serialization of Model objects

Source: Internet
Author: User

When writing the API, the data is generally transmitted in JSON format, and no objects can be used directly. At this time, the serialization of data conversion is very important, eloquent provides a very convenient method and conventions, not only can be converted, but also control the key values inside.

Basic usage transforms a model into arrays
    1. $user = App\User::with(‘roles‘)->first();
    2. return $user->toArray();

Convert a model into an array;

    1. $users = App\User::all();
    2. return $users->toArray();

Convert collection into arrays;

Convert the model to JSON
    1. $user = App\User::find(1);
    2. return $user->toJson();

This is a manual function;

If you return directly to a model or collection, the system will automatically cast it into JSON:

    1. Route::get(‘users‘, function () {
    2. return App\User::all();
    3. });

You can test the results with a route.

Hide some of the properties in JSON

A field like password, in fact, does not want to appear in the JSON for others to see, then you can hide it

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class User extends Model
  4. {
  5. protected $hidden = [‘password‘];
  6. }

This is the blacklist;

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class User extends Model
  4. {
  5. protected $visible = [‘first_name‘, ‘last_name‘];
  6. }

This is a white list;

The document is written in red letters:

    1. Note: When hiding relationships, use the relationship‘s method name, not its dynamic property name.

Because some tables are related to queries, you can hide these tables, but fill in the filter field above, do not fill in the field of the related table, but fill in the model of the relationship between the method, so that the entire table can be hidden, if you want to hide the related tables of some fields, then to the association table to hide. (I wanted to write an example, my wife told me to go to breakfast, not to write.) )

Add some of the properties in the JSON

Just said to remove the attribute, obviously sometimes you also need to manually add some properties:

The first step is to add a accessor:

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class User extends Model
  4. {
  5. public function getIsAdminAttribute()
  6. {
  7. //return $this->attributes[‘admin‘] == ‘yes‘;
  8. //官网是这样写的,何必增加理解难度,让人混淆呢,真是的;
  9. //我简写成这样了
  10. return ‘yes‘;
  11. }
  12. }

In the second step, add the added fields to the $appends array:

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class User extends Model
  4. {
  5. public function getIsAdminAttribute()
  6. {
  7. //return $this->attributes[‘admin‘] == ‘yes‘;
  8. //官网是这样写的,何必增加理解难度,让人混淆呢,真是的;
  9. //我简写成这样了
  10. return ‘yes‘;
  11. }
  12. protected $appends = [‘is_admin‘];
  13. }

Okay, there's one more in the JSON.is_admin: "yes"

Laravel 5.1 Document Guide--eloquent: Serialization of Model objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.