mercredi 3 avril 2019

Retrieving relationships of relationships using Eloquent

I have a database with the following tables and relationships: invoice ,order & product each factor has several orders,and every orders point to a product.


Invoice Model:

class Invoice extends Model
{
    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}


Order Model:

class order extends Model
{
    public function invoice()
    {
        return $this->belongsTo(Invoice::class);
    }
    public function product()
    {
        return $this->belongsTo('App\Product');
    }   
}


Product Model:

class product extends Model
{
    public function order()
    {
        return $this->hasOne('App\Order');
    }
}


the name of each order is foreign key to the product id,

$table->unsignedBigInteger('name')->references('id')->on('products')->default(0);

in my template i can showing the invoice with orders like this:




@foreach($invoice->orders as $order)
    
    $
    
    $
@endforeach

with this function:

public function show($id)
{
    $invoice = Invoice::with('orders')->findOrFail($id);
    return view('admin.invoice.show.main', compact('invoice'));
}

how can show the name of product in orders recorde like this:



im using before for single loop(e.g., product & category) but in this example we have 3 relation and using a compact method before.



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

Aucun commentaire:

Enregistrer un commentaire