jeudi 26 mars 2020

Join 3 tables in Laravel to get one attribute from each one

I have three tables

warehouses
PK warehouse_id
warehouse_name

good_quantities
PK good_quantity_id
FK good_id
FK warehouse_id
quantity

goods
PK good_id
good_name

And i wrote their models with the relationships with each others

Good Model

class Good extends Model
{
/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'goods';

/**
 * Get the Good Quantity for this Good.
 */
public function good_quantities()
{
    return $this->hasMany('App\Models\Good_Quantity','good_id');
}

}

Warehouse Model

class Warehouse extends Model
{
/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'warehouses';


/**
 * Get the Goods Quantity for this Warehouse.
 */
public function good_quantities()
{
    return $this->hasMany('App\Models\Good_Quantity','warehouse_id');
}

}

Good_Quantity Model

class Good_Quantity extends Model
{
/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'good_quantities';

/**
 * Get the Good's Info.
 */
public function good()
{
    return $this->belongsTo('App\Models\Good','good_id');
}

/**
 * Get the Good Quantity's Warehouse.
 */
public function warehouse()
{
    return $this->belongsTo('App\Models\Warehouse','warehouse_id');
}

}

So i'm trying to get all the good quantities records in a certain warehouse, And want to display it in a View like this:

| good_name | warehouse_name | quantity |

What is the right query to get this result with Eloquent?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/39mcKZi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire