mercredi 7 juin 2017

Eloquent laravel : Get JSON representation of entry in table with all his relations

Model Ov.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Ov extends Model 
{

    protected $table = 'ov';
    public $timestamps = true;
    protected $fillable = array('version', 'macAddress');
    protected $hidden = array('timestamps');

    public function masters()
    {
        return $this->hasMany('App\Models\MasterEquipment', 'id_ov_foreign_key');
    }

}

Model MasterEquipment.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MasterEquipment extends Model 
{

    protected $table = 'masterEquipment';
    public $timestamps = true;
    protected $fillable = array('macAddress');

    public function slaveEquipments()
    {
        return $this->hasMany('App\Models\SlaveEquipment', 'id_masterEquipment_foreign_key');
    }

    public function equipment()
    {
        return $this->hasOne('App\Models\Equipment', 'id_masterEquipment_foreign_key');
    }

}

I would like to retrieve the entire JSON representation for a given version of OV: I tried :

return Ov::where('version', '3')->with('masters')->get();

But it doesn't work, i get only the following json: (with masters empty)

{ "version": 3, "macAddress": "00:01:02:04:04", "masters":[] }

Could you help me please ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire