virtual
5 days ago

Fetch nested table data in Laravel

Posted 5 days ago by virtual

Hi, I have faced a problem. I am want to fetch data from multiple table.But cuold not do this. Below i am given my elequent relationships. Model- Order

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $guarded=[];
    
    public function orderItems()
    {
        return $this->hasMany(OrderItem::class);
    }
}

model-OrderItem

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
    protected $guarded=[];
    public function products()
    {
        return $this->belongsTo(Product::class);
    }
   
    public function orders()
    {
        return $this->belongsTo(Order::class);
    }
}

Model-Product

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $guarded=[];
    public function user()
    {
        return $this->belongsTo(User::class);
    }
  
  
    public function orderItem()
    {
        return $this->hasMany(OrderItem::class);
    }
  
}

And my controller method

public function completeOrder()
{
  
    $orders  = Order::with(['orderItems','orderItems.products'])->where('user_id',Auth::user()->id)->get();
    return $orders;
     return view('frontEnd.cart.completeOrder',compact('cartItem'));
}

Result---

[{"id":1,"user_id":11,"orderNumber":"#00000012","total":"3,525.94","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 04:50:49","updated_at":"2019-09-11 04:50:49","order_items":[]},{"id":2,"user_id":11,"orderNumber":"#00000012","total":"15,634.41","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 04:54:32","updated_at":"2019-09-11 04:54:32","order_items":[]},{"id":3,"user_id":11,"orderNumber":"#00000012","total":"15,634.41","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 05:00:01","updated_at":"2019-09-11 05:00:01","order_items":[]},{"id":4,"user_id":11,"orderNumber":"#00000012","total":"5,302.22","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 05:04:34","updated_at":"2019-09-11 05:04:34","order_items":[]},{"id":5,"user_id":11,"orderNumber":"#00000012","total":"1,210.00","payment":"ssfs","transaction_id":"978678","process":"pending","processed_by":null,"created_at":"2019-09-11 05:07:00","updated_at":"2019-09-11 05:07:00","order_items":[]},{"id":6,"user_id":11,"orderNumber":"#00000012","total":"2,214.30","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 05:18:04","updated_at":"2019-09-11 05:18:04","order_items":[]},{"id":7,"user_id":11,"orderNumber":"#00000012","total":"2,214.30","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 05:19:53","updated_at":"2019-09-11 05:19:53","order_items":[]},{"id":8,"user_id":11,"orderNumber":"#00000012","total":"4,951.32","payment":"ssfs","transaction_id":null,"process":"pending","processed_by":null,"created_at":"2019-09-11 12:22:51","updated_at":"2019-09-11 12:22:51","order_items":[{"id":12,"order_id":8,"product_id":6,"discount":"12","tax":"859.32","subtotal":"432","quantity":1,"created_at":"2019-09-11 12:22:51","updated_at":"2019-09-11 12:22:51","products":null},{"id":13,"order_id":8,"product_id":7,"discount":"12","tax":"859.32","subtotal":"3660","quantity":2,"created_at":"2019-09-11 12:22:51","updated_at":"2019-09-11 12:22:51","products":null}]},{"id":9,"user_id":11,"orderNumber":"#00000012","total":"866.36","payment":"ssfs","transaction_id":"78965","process":"pending","processed_by":null,"created_at":"2019-09-11 13:17:04","updated_at":"2019-09-11 13:17:04","order_items":[{"id":14,"order_id":9,"product_id":3,"discount":"12","tax":"150.36","subtotal":"284","quantity":1,"created_at":"2019-09-11 13:17:04","updated_at":"2019-09-11 13:17:04","products":null},{"id":15,"order_id":9,"product_id":6,"discount":"12","tax":"150.36","subtotal":"432","quantity":1,"created_at":"2019-09-11 13:17:04","updated_at":"2019-09-11 13:17:04","products":null}]},{"id":10,"user_id":11,"orderNumber":"#00000012","total":"0.00","payment":"ssfs","transaction_id":"78965","process":"pending","processed_by":null,"created_at":"2019-09-11 13:17:58","updated_at":"2019-09-11 13:17:58","order_items":[]},{"id":11,"user_id":11,"orderNumber":"#00000012","total":"0.00","payment":"ssfs","transaction_id":"78965","process":"pending","processed_by":null,"created_at":"2019-09-11 13:19:23","updated_at":"2019-09-11 13:19:23","order_items":[]},{"id":12,"user_id":11,"orderNumber":"#00000012","total":"0.00","payment":"ssfs","transaction_id":"78965","process":"pending","processed_by":null,"created_at":"2019-09-11 13:19:35","updated_at":"2019-09-11 13:19:35","order_items":[]},{"id":13,"user_id":11,"orderNumber":"#00000012","total":"0.00","payment":"ssfs","transaction_id":"78965","process":"pending","processed_by":null,"created_at":"2019-09-11 13:19:43","updated_at":"2019-09-11 13:19:43","order_items":[]},{"id":14,"user_id":11,"orderNumber":"#00000012","total":"2,214.30","payment":"ssfs","transaction_id":"2423","process":"pending","processed_by":null,"created_at":"2019-09-11 14:04:20","updated_at":"2019-09-11 14:04:20","order_items":[{"id":16,"order_id":14,"product_id":7,"discount":"12","tax":"384.30","subtotal":"1830","quantity":1,"created_at":"2019-09-11 14:04:20","updated_at":"2019-09-11 14:04:20","products":null}]}]

I want to fetch all product data but can not do this. How to solve this problem. note in the order table user_id column exist. Thanks in advanced.

Please sign in or create an account to participate in this conversation.