[Laravel 5.2 Documentation] Eloquent ORM-serialization

Source: Internet
Author: User
Tags tojson

1. Introduction

When building Jsonapi, it is often necessary to convert models and association relationships to arrays or JSON. Eloquent includes a convenient way to implement these transformations, and to control which properties are included in the serialization.

2. Basic use

The conversion model is an array

To convert a model and its associated relationships to an array, you can use the ToArray method. This method is recursive, so all properties and their associated object properties (including associated associations) are converted to arrays:

$user = App\user::with (' roles ')->first (); return $user->toarray ();

You can also convert collections to arrays:

$users = App\user::all (); return $users->toarray ();

Conversion model to JSON

To convert the model to JSON, you can use the Tojson method, like ToArray, where the Tojson method is recursive, and all attributes and their associated properties are converted to JSON:

$user = App\user::find (1); return $user->tojson ();

You can also convert a model or collection into a string, which will automatically call the Tojson method:

$user = App\user::find (1); return (string) $user;

Since models and collections are converted to JSON when converted to strings, you can return eloquent objects directly from your app's routing or controller:

Route::get (' Users ', function () {    return App\user::all ();});

3. Hide attributes in JSON

Sometimes you want to hide certain properties, such as passwords, in a model array or JSON display, to implement this, set $hidden properties when defining the model:

     

Note: If you want to hide the association relationship, use the method name of the association relationship instead of the dynamic property name.

In addition, you can use the Visible property to define a whitelist of properties for the model array and JSON display:

      

temporarily exposing hidden properties

If you want to temporarily display hidden properties in a particular model, you can use the Makevisible method, which returns the model instance as a chain of methods:

return $user->makevisible (' attribute ')->toarray ();

4. Append value to JSON

Sometimes, you need to add fields that are not in the database to the array, to do this, first define an accessor for this value:

 
    attributes[' admin '] = = ' yes ';    }}

After the accessor is defined, add the field name to the model's appends property:

!--? phpnamespace app;use Illuminate\database\eloquent\model;class User extends    model{/** * Append to model array form accessors * * @var array */protected $appends = [' Is_admin '];}  After the 

field is added to the appends list, it will be included in the model array and JSON, and the fields in the appends array will also follow the visible and hidden settings configured in the model.

  • Related Article

    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.