Finding a Value in Pivot Relation Not Working
Hello All,
For practising Laravel, I'm developing an Ecommerce site where in user's can order foods online. I'm stuck at updating the cart items in database. Below are the tables involved:
Users, Items and a Pivot Table Cart with (user_id, item_id, quantity) with Many to Many Relation, ofcourse:
Below is the relation and update method defined in user model:
public function item()
{
return $this->belongsToMany('App\Item', 'cart')->withPivot('quantity');
}
public function getCart()
{
return $this->item()->get();
}
public function addToCart($item_id, $qty=1)
{
$this->item()->attach($item_id, ['quantity'=>$qty]);
}
public function addMultiToCart($arrItems)
{
$this->item()->attach($arrItems);
}
public function arrUpdCartItem($arrItems)
{
foreach($arrItems as $id=>$attr)
{
$this->item()->updateExistingPivot($id, $arrItems);
}
}
Now what i'm doing is; first i fetch the cart items from DB and then checking in the cart items from session:
-
If an item in the session does not exist in DB, then i will insert that item in DB
-
If an item in the session does exist in DB, then i will check if the quantity selected for that item is same in session as well as in DB. If yes, then ignore this item else update the quantity in DB from session value
With above 2 actions I want to make sure the cart detail is same in session as well as in DB. Below is the code to do this:
// $arrCartItems is the array which I populate above and contains items from session cart
$arrDBCartItems = $user->getCart();
$arrNewCartItem = array();
$arrUpdCartItem = array();
foreach($arrCartItems as $itemid=>$cartdetails)
{
// CHECK IF CURRENT ITEM FROM SESSION CART DO EXIST IN DB
if($arrDBCartItems->item->contains('item_id', $itemid)) // THIS LINE IS NOT WORKING
{
$currItem = $arrDBCartItems->where('item_id', $itemid)->first();
// CHECK IF QUANTITY FOR THIS ITEM IS NOT SAME IN SESSION AS WELL AS DB
if($cartdetails['quantity'] != $currItem->quantity)
{
// UPDATE THE QTY FOR THIS ITEM IN DB
$arrUpdCartItem[$itemid] = array('quantity'=>$cartdetails['quantity']);
}
}
else
{
// NEW ITEMS FROM SESSION CART TO BE ADDED IN DB
$arrNewCartItem[$itemid] = array('quantity'=>$cartdetails['quantity']);
}
}
// INSERT OR UPDATE ACTION CODES GOES AHEAD
I've got an item with id 10 in DB and session as well with quantity=1. Now if i update the quantity it works well in session but not in DB. The line in FOR LOOP where I'm checking that DB collection CONTAINS this item id is giving me error as
Property [item] does not exist on this collection instance
I don't know where I'm going wrong as I can see pivot relation if i user dd($arrDBCartItems)
2 => App\Item {#286
#connection: "mysql"
#table: "items"
#primaryKey: "id"
#attributes: array:16 [
"id" => 10
"name" => "Palak Paneer"
"slug" => "palak-paneer"
"list_img" => "10.jpg"
"cart_img" => "10.jpg"
"detail_img" => "1.jpg"
"summary" => ""
"detail" => ""
"serving" => "3 persons"
"price" => "100"
"category_id" => 1
"subtype" => ""
"display_order" => 10
"created_at" => "2019-12-09 18:22:38"
"updated_at" => null
"deleted_at" => null
]
#original: array:19 [
"id" => 10
"name" => "Palak Paneer"
"slug" => "palak-paneer"
"list_img" => "10.jpg"
"cart_img" => "10.jpg"
"detail_img" => "1.jpg"
"summary" => ""
"detail" => ""
"serving" => "3 persons"
"price" => "100"
"category_id" => 1
"subtype" => ""
"display_order" => 10
"created_at" => "2019-12-09 18:22:38"
"updated_at" => null
"deleted_at" => null
"pivot_user_id" => 1
"pivot_item_id" => 10
"pivot_quantity" => 1
]
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#277
+incrementing: false
#guarded: []
#connection: "mysql"
#table: "cart"
#primaryKey: "id"
#keyType: "int"
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [
"user_id" => 1
"item_id" => 10
"quantity" => 1
]
#original: array:3 [
"user_id" => 1
"item_id" => 10
"quantity" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
+pivotParent: App\User {#260}
#foreignKey: "user_id"
#relatedKey: "item_id"
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#forceDeleting: false
}
Can any one please help me in this?
Javed.
Please or to participate in this conversation.