Showing nested relationships details

Posted: 2014-06-09 17:25:47

This is soooo easy but this is always something this easy can be missed. In this example we have a Property Model with Buildings that have an address.

So the model files look like this


    //structures = buildings
    public function structures()
        return $this->hasMany('Structure');
    public function address()
        return $this->belongsTo("Address");

So when I query a property I now get

            "id": 1,
            "structures": [
                    "id": 6,
                    "address_id": 9,
                    "address": {
                        "id": 9,
                        "city": "Melvinport",
                        "state": "MT",
                        "zip": "75182",
                        "lat": -18.370000839233,
                        "lng": 36.270000457764,
                        "created_at": "2014-06-09 16:18:40",
                        "updated_at": "2014-06-09 16:18:40"

Then in my query for properties I just included this with statement.

public function search($args) { $term = $args['terms']; $found = $this->property->with('structures.address') ->where("name", "LIKE", "%{$term}%") ->get(); return $found; }

the dot notation basically showing load Structures and the Address data related to those.