Skip to content

Commit 046a0b5

Browse files
authored
Update README.md
1 parent 8488bb2 commit 046a0b5

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --
4646

4747
## HOW TO USE
4848

49+
- [Quick Usage](#usage-usage-example)
4950
- [Usage](#usage)
5051
- [Conditions](#conditions)
5152
- [Items](#items)
@@ -58,6 +59,51 @@ php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --
5859
- [Using Different Storage](#storage)
5960
- [License](#license)
6061

62+
## Quick Usage Example
63+
64+
```php
65+
// Quick Usage with the Product Model Association & User session binding
66+
67+
$Product = Product::find($productId); // assuming you have a Product model with id, name, description & price
68+
$rowId = 456; // generate a unique() row ID
69+
$userID = 2; // the user ID to bind the cart contents
70+
71+
// add the product to cart
72+
Cart::session($userID)->add(array(
73+
'id' => $rowId,
74+
'name' => $Product->name,
75+
'price' => $Product->price,
76+
'quantity' => 4,
77+
'attributes' => array(),
78+
'associatedModel' => $Product
79+
));
80+
81+
// update the item on cart
82+
Cart::session($userID)->update($rowId,[
83+
'quantity' => 2,
84+
'price' => 98.67
85+
]);
86+
87+
// delete an item on cart
88+
Cart::session($userID)->remove($rowId);
89+
90+
// view the cart items
91+
$items = Cart::getContent();
92+
foreach($items as $row) {
93+
94+
echo $row->id; // row ID
95+
echo $row->name;
96+
echo $row->qty;
97+
echo $row->price;
98+
99+
echo $row->model->id; // whatever properties your model have
100+
echo $row->model->name; // whatever properties your model have
101+
echo $row->model->description; // whatever properties your model have
102+
}
103+
104+
// FOR FULL USAGE, SEE BELOW..
105+
```
106+
61107
## Usage
62108

63109
### IMPORTANT NOTE!
@@ -127,7 +173,7 @@ Cart::add(455, 'Sample Item', 100.99, 2, array());
127173

128174
// array format
129175
Cart::add(array(
130-
'id' => 456,
176+
'id' => 456, // inique row ID
131177
'name' => 'Sample Item',
132178
'price' => 67.99,
133179
'quantity' => 4,
@@ -157,7 +203,14 @@ Cart::add(array(
157203

158204
// add cart items to a specific user
159205
$userId = auth()->user()->id; // or any string represents user identifier
160-
Cart::session($userId)->add(455, 'Sample Item', 100.99, 2, array());
206+
Cart::session($userId)->add(array(
207+
'id' => 456, // inique row ID
208+
'name' => 'Sample Item',
209+
'price' => 67.99,
210+
'quantity' => 4,
211+
'attributes' => array(),
212+
'associatedModel' => $Product
213+
));
161214

162215
// NOTE:
163216
// Please keep in mind that when adding an item on cart, the "id" should be unique as it serves as

0 commit comments

Comments
 (0)