mardi 10 septembre 2019

Laravel Relationship between Three Models

sold_items:
id    order_id   customer_id   name      
--    --------   -----------   ----
1     5          14            An object

orders:
id      po_num
--      ------
...     ...
5       123


customers:
id      name
--      ----
...     ...
14      John Doe

A SoldItem has an order_id and a customer_id.

I'm trying to define a hasOne relationship between an Order and its Customer through the SoldItem without having to insert a customer_id column in the Order model's table.

hasOneThrough seemed to be the solution but I couldn't figure out where to go from there, or is that even the answer?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    public $timestamps = false;
    protected $guarded = ['id'];

    public function sold_items()
    {
        return $this->hasMany('App\SoldItem');
    }

    public function customer()
    {
        // return $this->hasOneThrough();
    }
}




via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire