简体   繁体   中英

How to get data from Laravel nested relationship?

I am new to Laravel. I'm having a lot of trouble getting a very-nested relationship to work correctly in laravel.

The wanted behaviour is as follows,

I have 5 Models. Purchase , Inventory , Slab , Scarting and FloorTile .

I have one to one relation between Purchase and Inventory Models, while inventory have further relation with slab, scarting and floortile.

I have purchase id and want to get data from inventory table, then get a slab_id , scarting_id or floorTile_id from inventory table and get data from the respective table.

The slab_id , scarting_id and floorTile_id can be multiple against inventory id .

I don't know how to get data every iteration.

Purchase Model:

<?php

namespace App;
use App\Inventory;

use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
    public function inventory()
    {
        return $this->hasOne('App\Inventory');
    }

}

Inventory Model:

<?php

namespace App;
use App\Slab;
use App\Scarting;
use App\FloorTile;
use App\Purchase;
Use App\StandardSize;

use Illuminate\Database\Eloquent\Model;

class Inventory extends Model
{
    public function purchase()
    {
        return $this->belongsTo('App\Purchase');
    }

    public function standatdSize()
    {
        return $this->hasOne('App\StandardSize');
    }

    public function slab()
    {
        return $this->hasOne('App\Slab');
    }

    public function scarting()
    {
        return $this->hasOne('App\Scarting');
    }

    public function floorTile()
    {
        return $this->hasOne('App\FloorTile');
    }
}

I have tried this:

$purchase = Purchase::where('factoryName', $factoryName)->with('inventory.slab')->get();

The result is as follows:

{
  "id": 36,
  "fname": "Sama",
  "lname": "Jojo",
  "factoryName": "Sama Inc.",
  "cnic": "3216542",
  "area": "Johar town",
  "city": "Lahore",
  "province": "Punjab",
  "phone1": "45678912345",
  "phone2": "45678912345",
  "inventory_ID": 10,
  "created_at": "2019-03-19 12:11:45",
  "updated_at": "2019-03-19 12:11:45",
  "inventory": {
    "id": 10,
    "purchase_id": 36,
    "marbleType": "1",
    "totalSquareFt": 25,
    "priceperSquareFt": 230,
    "totalPurchasePrice": 25000,
    "standardSize_ID": "5",
    "salePrice": 250,
    "miliMeter": "6mm",
    "slab_ID": 1,
    "scarting_ID": null,
    "floorTile_ID": null,
    "created_at": "2019-03-19 12:11:45",
    "updated_at": "2019-03-19 12:11:45",
    "slab": null
  }
}

Even there is data available against slab but still its showing NULL.

It would be easier for you if your records were slab_id and not slab_ID this way Laravel would do it for you, however you have not followed Laravel's naming convention which is fine but you'll have to tell Laravel that you're using your own naming conventions and it will pick it up just fine. I've provided an example below.

public function slab()
{
    return $this->hasOne('App\Slab', 'id', 'slab_ID');
}

$this->hasOne('App\\Model', 'foreign_key', 'local_key');

The foreign key is the primary key of the model you're trying to retrieve and the local_key is the reference of what it's called in this model, usually model_id and in this case would be slab_ID .

local_key = $this->id and foreign_key = App\\Slab in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM