Skip to content

Commit 7ef07de

Browse files
committed
updated docs for using custom storage
1 parent 4e65717 commit 7ef07de

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ For Laravel 5.4~:
4343
* [Events](#events)
4444
* [Format Response](#format)
4545
* [Examples](#examples)
46+
* [Using Different Storage](#storage)
4647
* [Changelogs](#changelogs)
4748
* [License](#license)
4849

@@ -721,6 +722,81 @@ $items->each(function($item)
721722

722723
```
723724

725+
## Using Different Storage
726+
727+
Using different storage for the carts items is pretty straight forward. The storage
728+
class that is injected to the Cart's instance will only need methods.
729+
730+
Example we will need a wishlist, and we want to store its key value pair in database instead
731+
of the default session. We do this using below:
732+
733+
Create a new class for your storage:
734+
735+
Eg.
736+
```
737+
class WishListDBStorage {
738+
739+
public function has($key)
740+
{
741+
// your logic here to check if storage has the given key
742+
}
743+
744+
public function get($key)
745+
{
746+
// your logic here to get an item using its key
747+
}
748+
749+
public function put($key, $value)
750+
{
751+
// your logic here to put an item with key value pair
752+
}
753+
}
754+
```
755+
756+
Then in your service provider for your wishlist cart, you replace the storage
757+
to use your custom storage.
758+
759+
```
760+
use Darryldecode\Cart\Cart;
761+
use Illuminate\Support\ServiceProvider;
762+
763+
class WishListProvider extends ServiceProvider
764+
{
765+
/**
766+
* Bootstrap the application services.
767+
*
768+
* @return void
769+
*/
770+
public function boot()
771+
{
772+
//
773+
}
774+
/**
775+
* Register the application services.
776+
*
777+
* @return void
778+
*/
779+
public function register()
780+
{
781+
$this->app->singleton('wishlist', function($app)
782+
{
783+
$storage = new WishListDBStorage(); <-- Your new custom storage
784+
$events = $app['events'];
785+
$instanceName = 'cart_2';
786+
$session_key = '88uuiioo99888';
787+
return new Cart(
788+
$storage,
789+
$events,
790+
$instanceName,
791+
$session_key,
792+
config('shopping_cart')
793+
);
794+
});
795+
}
796+
}
797+
```
798+
799+
724800
## Changelogs
725801

726802
**2.4.0

0 commit comments

Comments
 (0)