0

Im working with Laravel 4 with MySQL and using relationships methods to access data of related tables. I have the following related tables:

team table:

id INT
partnet_id INT  -- Related many-to-one with partner table
name VARCHAR(50)

partner table:

id INT
name VARCHAR(50)
-- more data about only to partners

guest table:

id INT
name VARCHAR(50)
-- more data about only to guests

And member table:

id INT
partner_id INT -- Related many-to-one to partner table
guest_id INT -- Related many-to-one to guest table
team_id INT -- Related many-to-one to team table

So, I want to get a team (or all of them) with their members with their related data with guest or partner.

The output should be something like this:

{ 
    "id": 1,
    "partner": {
        "id": 1,
        "name":"Hello world",
        "partner_number":1111,
        "handicap":12
    },
    "name":"my team,
    "members": [
        {
            "id": 1,
            "name":"Hello world",
            "partner_number":1111,
            "handicap":12,
            "type": "partner"
        },
        {
            "id": 2,
            "name": "Cat Kitten",
            "partner_number":2222,
            "handicap":12,
            "type": "partner"
        },
        {
            "id": 3,
            "name": "Bruce Wayne",
            "partner_number":3333,
            "handicap":10,
            "type": "partner"
        },
        {
            "id": 1,
            "name": "The Joker",
            "idcard":21841933,
            "handicap":10,
            "type": "guest"
        }
    ]
}

I try to get that output with this:

$teams = Team::find(1);
$teams->load('partner', 'member.partner', 'member.guest');

With that I get the needed data but with stuff I do not really want/need:

Array
(
    [id] => 2
    [partner_id] => 2
    [name] => 'my team'
    [partner] => Array
        (
            [id] => 2
            [name] => Batman Batman
            [partner_number] => 2222
            [handicap] => 8
        )
    [members] => Array
        (

            [1] => Array
                (
                    [id] => 5           // I don't need this
                    [team_id] => 2      // I don't need this
                    [partner_id] => 3   // I don't need this
                    [guest_id] =>       // I don't need this
                    [partner] => Array
                        (
                            [id] => 3
                            [name] => Amazing Spiderman
                            [partner_number] => 3333
                            [handicap] => 12
                        )

                    [guest] =>          // I don't need this
                )

            [2] => Array
                (
                    [id] => 6           // I don't need this
                    [team_id] => 2      // I don't need this
                    [partner_id] =>     // I don't need this    
                    [guest_id] => 1     // I don't need this
                    [partner] =>        // I don't need this
                    [guest] => Array
                        (
                            [id] => 1
                            [name] => Eddard Stark
                            [idcard] => 2139823
                            [handicap] => 21
                        )

                )

        )

)

Where is commented // I don't need this is data related to member table but I only need the inner relation data (partner or guest data). For intance, if I do Member::find(1)->partner I get the partner data without stuff of member table. Is it possible to do something like that with eager load or with another method in Laravel?

I know I can manipulate and iterate on the output and keep only the necessary data but I just want to know if it's possible without making more queries or doing heavy loops.

Any hint, help or comment will be useful.

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
Darwing
  • 833
  • 1
  • 12
  • 23

1 Answers1

0

Try using the $visible and $hidden array in your model classes.

From the docs:

Sometimes you may wish to limit the attributes that are included in your model's array or JSON form, such as passwords. To do so, add a hidden property definition to your model:

Hiding Attributes From Array Or JSON Conversion

class User extends Eloquent {

    protected $hidden = array('password');

}

Alternatively, you may use the visible property to define a white-list:

protected $visible = array('first_name', 'last_name');

See here: http://four.laravel.com/docs/eloquent

Glad To Help
  • 5,299
  • 4
  • 38
  • 56