My PostOrder Function
public function getPaymentStatus(Request $request){
if ($result->getState() == 'approved') {
$user_id = Auth::user()->id;
$user_email = Auth::user()->email;
//get shipping details
$shippingDetails = DeliveryAddress::where(['user_email'=>$user_email])->first();
$order = new Order;
$order->user_id = $user_id;
$order->user_email = $user_email;
$order->name = $shippingDetails->name;
$order->address = $shippingDetails->address;
$order->city = $shippingDetails->city;
$order->state = $shippingDetails->state;
$order->country = $shippingDetails->country;
$order->postcode = $shippingDetails->postcode;
$order->mobile = $shippingDetails->mobile;
$order->coupon_code = $coupon_code;
$order->coupon_amount = $coupon_amount;
$order->order_status = "New";
$order->grand_total = Session::get('grand_total');
$order->save();
$order_id = DB::getPdo()->lastInsertId();
$cartProducts = DB::table('cart')->where(['user_email'=>$user_email])->get();
foreach($cartProducts as $pro){
$cartPro = new OrdersProduct;
$cartPro->order_id = $order_id;
$cartPro->user_id = $user_id;
$cartPro->product_id = $pro->product_id;
$cartPro->product_code = $pro->product_code;
$cartPro->product_name = $pro->product_name;
$cartPro->product_size = $pro->size;
$cartPro->product_color = $pro->product_color;
$cartPro->product_price = $pro->price;
$cartPro->product_qty = $pro->quantity;
$cartPro->save();
}
return redirect('/thank-you');
}
i have a table "products" which store product details and a table "products_attribute" which store the stock of the product. I don't know how link this to products_attribute table. Please Help.
It looks like you already have a grasp of Eloquent
Models with your Order
and DeliveryAddress
model.
To setup relationships between your Product
and Product Attribute
simply define the relationship method inside your Product
model:
<?php
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function attributes()
{
return $this->hasMany('ProductAttribute');
}
}
Eloquent will use snake case in determining the name of foreign key, which in this case would be product_id
. If your foreign key is defined differently you may specify the name as a second argument:
return $this->hasMany('ProductAttribute', 'my_product_id');
If you want to have a relationship from ProductAttribute
to Product
use the belongsTo
method:
<?php
use Illuminate\Database\Eloquent\Model;
class ProductAttribute extends Model
{
public function product()
{
return $this->belongsTo('Product');
}
}
You may also specify a custom foreign key name similar to the hasMany
method:
return $this->belongsTo('Product', 'my_product_id');
To update the quauntity you could do something like this:
foreach($cartProducts as $pro){
$cartPro = new OrdersProduct;
$cartPro->order_id = $order_id;
$cartPro->user_id = $user_id;
$cartPro->product_id = $pro->product_id;
$cartPro->product_code = $pro->product_code;
$cartPro->product_name = $pro->product_name;
$cartPro->product_size = $pro->size;
$cartPro->product_color = $pro->product_color;
$cartPro->product_price = $pro->price;
$cartPro->product_qty = $pro->quantity;
$cartPro->save();
$product = Product::find($pro->product_id);
if (null !== $product) {
// you will need a sku or some other unique identifier
// from your attribute to determine the correct attribute
// to modify after your order is complete
$attribute = $product->attributes()-where('sku', $sku)->first();
$attribute->quantity = $attribute->quantity - $pro->quantity;
$attribute->save();
}
}
you should use relationships here
first you create ProductModel after that you create one method like attributes and use hasMany relationships so you can do whatever you want