/EloquentSerializer

Symfony Serializer bridge for the Eloquent ORM.

Primary LanguagePHPMIT LicenseMIT

EloquentSerializer

Package version Build Status License

Package to allow Symfony Serializer to work on Eloquent models.

Table of Contents

  1. Install
    1. Laravel
    2. Symfony
  2. Usage
  3. Contributing

Install

You can use Composer to install the bundle to your project:

composer require theofidry/eloquent-serializer

Laravel (~5.5.0)

Nothing to do, the provider is automatically discovered :)

Symfony (^3.4 || ^4.0)

Enable the bundle by updating your app/AppKernel.php file to enable the bundle:

<?php
// app/AppKernel.php

public function registerBundles()
{
    //...
    $bundles[] = new \Fidry\EloquentSerializer\Bridge\Symfony\FidryEloquentSerializerBundle();

    return $bundles;
}

Usage

use Fidry\EloquentSerializer\Bridge\Laravel\Facade\Serializer;

// Dummy is an Eloquent model
$dummy = Dummy::create([
    'id' => 100,
    'name' => 'Gunner Runte',
    'email' => 'vbrekke@example.com',
    'password' => '$2y$10$j/R4kRrymk3wMXwohvoRou2zBKJZVecr1VON.9NnSXu24k6CP6tDe',
    'remember_token' => 'PhiasHkmCh',
    'created_at' => new Carbon('2016-07-02T12:28:14+00:00'),
]);

// You can either use the Facade (Laravel) or the 'serializer' service (Laravel & Symfony)
$normalizedDummy = Serializer::normalize($dummy);

// $normalizedDummy: [
//     'id' => 100,
//     'name' => 'Gunner Runte',
//     'email' => 'vbrekke@example.com',
//     'created_at' => '2016-07-02T12:28:14+00:00',
// ];

Contributing

You can find two bash scripts to help with the testing:

bin/setup   # Install the dependencies and setup the sqlite database
bin/tests   # Runs the tests