I am a beginner level of laravel. I am creating a warehouse management in laravel. I want to associate 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 function 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 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:
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:
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 client
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;
}
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);