Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

order list not view properly in laravel api

i am a begginer level of laravel.i am doing the warehouse management in laravel i want to assiocate the relationship with many to many and one to many when i view the data i am struck on this line i don’t how to get details form product order is it correct i need to get the qty on the product order. can any one help me to complete index funtion of ordercontroller load. client table and order table and product order how to view the data as json :

 foreach ($order->products as $product) 
            {
                $arrayobj['order_products'][] = [
                    'product_id' => $product->id,
                    'product_name' => $product->name, 
                    

                ];

            }

Here is the Full code :
here is code of product and client and order and order product i need you to correct with order api part index and store function inside the order controller :

client

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Client extends Model
{
    use HasFactory;

    protected $primary_key = "id";
    protected $table = "clients";
    protected $fillable = [
        'name',
        'email',
        'phone',
        'address',
    ];

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

product

class Product extends Model
{
    use HasFactory;
    protected $primary_key = "id";
    protected $table = "products";

    protected $fillable = [
        'product_code',
        'product_name',
        'stock_level',
        'price',
        'category_id',
    ];

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function orders()
    {
        return $this->belongsToMany(Order::class, 'product_orders');
    }

order

class Order extends Model
{
    use HasFactory;

    protected $primary_key = "id";
    protected $table = "orders";


    protected $fillable = [
        'order_number',
        'total',
        'client_id',  
    ];



    public function client()
    {
        return $this->belongsTo(Client::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class, 'product_orders');
    }

}

ProductOrder

class ProductOrder extends Model
{
    use HasFactory;
    protected $primary_key = "id";
    protected $table = "product_orders";

    protected $fillable = [
        'product_id',
        'order_id',
        'qty',
    ];
}

here is the order controller

// retrieve data from clinet
public function index()
{

    $orders = Order::with(['client', 'products'])->get();

    // create empty array
    $response_array = [];

    // loop orders

    foreach ($orders as $order) 
        {
                $arrayobj = [
                    'order_number' => $order->order_number,
                    'client_id' => $order->client_id,
                    'total' => $order->total,
                    'order_products' => []
                ];

    foreach ($order->products as $product) 
        {
            $arrayobj['order_products'][] = [
                'product_id' => $product->id,
                'product_name' => $product->name, 
            ];

        }


        // array_push()
    }

    return    $response_array;
}

>Solution :

You don’t need to create Model strictly for Pivot. It’s obligatory.

For Order model you can define relationship like:

public function products()
{
   return $this->belongsToMany(Product::class)->withPivot('qty');
}

and for Product:

public function orders()
{
   return $this->belongsToMany(Order::class)->withPivot('qty');
}

Then you can access to Pivot like:

foreach ($order->products as $product) {
        $orderData['order_products'][] = [
            /// ...
            'qty' => $product->pivot->qty // Access to the Pivot 
        ];
    }

    return response()->json($orderData);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading