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
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);