title: Laravel 5.6 API Resource not showing Relationship data
body: <p>I want to get relationship data into json using Resource in laravel 5.6
  <br><br>
  When I query, I get <code>response.data.created_by</code> as an object. <em>(first data marked in box)</em> <em>(I need this kind of functionality using API Resources)</em>
  <br>
  But with <a href="https://laravel.com/docs/5.6/eloquent-resources" rel="nofollow noreferrer">API Resources</a> it is only showing <code>id</code> and not <code>"created_by" object</code> in <code>response.response.data.created_by</code>. <em>(second data marked in box)</em>
  <br><br>
  *The data difference is marked inside box.
  <br>
  *The data is fetched using eager fetch.</p>
  
  <p>url: <code>http://localhost:8000/api/product/unit</code> <br>
  Response:</p>
  
  <pre>
  
      {
          "data": [
              {
                  "id": 1,
                  "unit": "Meter",
                  "symbol": "m",
                  "decimal": 1,
                  +----------------------------------------------------------------------------------+
                  |"created_by": {                                                                   |
                  |    "id": 1,                                                                      |
                  |    "name": "Admin",                                                              |
                  |    "email": "admin@gmail.com",                                                   |
                  |    "api_token": "$2y$10$.c7eJGS6x/C8JN9Hd.Qc1OgPUS8txMDuIHjZNBRRlHQVGrYbJcC5u",  |
                  |    "created_at": "2018-05-09 15:45:59",                                          |
                  |    "updated_at": "2018-06-08 15:38:41"                                           |
                  |},                                                                                |
                  +----------------------------------------------------------------------------------+
                  "updated_by": {
                      "id": 1,
                      "name": "Admin",
                      "email": "admin@gmail.com",
                      "api_token": "$2y$10$.c7eJGS6x/C8JN9Hd.Qc1OgPUS8txMDuIHjZNBRRlHQVGrYbJcC5u",
                      "created_at": "2018-05-09 15:45:59",
                      "updated_at": "2018-06-08 15:38:41"
                  },
                  "created_at": "2018-06-19 00:38:54",
                  "updated_at": "2018-06-19 20:00:16"
              }
          ],
          "resource": {
              "data": [
                  {
                      "id": 1,
                      "unit": "Meter",
                      "symbol": "m",
                      "decimal": 1,
                      +----------------+
                      |"createdBy": 1, |
                      +----------------+
                      "updatedBy": 1,
                      "createdAt": {
                          "date": "2018-06-19 00:38:54.000000",
                          "timezone_type": 3,
                          "timezone": "Asia/Kolkata"
                      },
                      "updatedAt": {
                          "date": "2018-06-19 20:00:16.000000",
                          "timezone_type": 3,
                          "timezone": "Asia/Kolkata"
                      }
                  }
              ]
          }
      }
  
  </pre>
  
  <p>UnitController.php:</p>
  
  <pre>
  
      namespace App\Http\Controllers\Product;
  
      use App\Models\Product\Unit;
      use Illuminate\Http\Request;
      use App\Http\Controllers\Controller;
      use Illuminate\Support\Facades\Validator;
      use App\Http\Resources\Product\UnitResourceCollection;
      use App\Http\Resources\Product\UnitResource;
      use Illuminate\Validation\ValidationException;
  
      class UnitController extends Controller {
          public function index()
          {
              $units = Unit::with(['created_by', 'updated_by'])->get();
              +------------------------------------------------------+
              |return [                                              |
              |    'data' => $units,                                 |
              |    'resource' => new UnitResourceCollection($units)  |
              |];                                                    |
              +------------------------------------------------------+
          }
      }
  
  </pre>
  
  <p>Unit Model:</p>
  
  <pre>
  
      namespace App\Models\Product;
      use Illuminate\Database\Eloquent\Model;
      class Unit extends Model
      {
          public function created_by() {
              return $this->belongsTo('App\User', 'created_by', 'id');
          }
  
          public function updated_by() {
              return $this->belongsTo('App\User', 'updated_by', 'id');
          }
      }
      </pre>
  
  <pre><code>UnitResource.php
  &lt;pre&gt;
  namespace App\Http\Resources\Product;
  
  use App\Http\Resources\UserResource;
  use Illuminate\Http\Resources\Json\JsonResource;
  
  class UnitResource extends JsonResource
  {
      public function toArray($request)
      {
          return [
              'id' =&gt; $this-&gt;id,
              'unit' =&gt; $this-&gt;unit,
              'symbol' =&gt; $this-&gt;symbol,
              'decimal' =&gt; $this-&gt;decimal,
              'createdBy' =&gt; $this-&gt;created_by,
              'updatedBy' =&gt; $this-&gt;updated_by,
              'createdAt' =&gt; $this-&gt;created_at,
              'updatedAt' =&gt; $this-&gt;updated_at
          ];
      }
  }
  </code></pre>
  
  <p></p>