From c22cdb9f7ae31781a1b65ad1868a9bfcd64fb26f Mon Sep 17 00:00:00 2001 From: bhavinjr Date: Tue, 12 Dec 2017 21:12:53 +0530 Subject: [PATCH] added initial commit --- .gitignore | 1 + README.md | 124 +++++ composer.json | 35 ++ composer.lock | 466 ++++++++++++++++++ config/wishlist.php | 15 + ...17_12_11_061154_create_wishlists_table.php | 24 + src/Facades/Wishlist.php | 14 + src/Models/Wishlist.php | 26 + src/Providers/WishlistServiceProvider.php | 36 ++ src/Wishlist.php | 62 +++ 10 files changed, 803 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 config/wishlist.php create mode 100644 database/migrations/2017_12_11_061154_create_wishlists_table.php create mode 100644 src/Facades/Wishlist.php create mode 100644 src/Models/Wishlist.php create mode 100644 src/Providers/WishlistServiceProvider.php create mode 100644 src/Wishlist.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/README.md b/README.md new file mode 100644 index 0000000..5bb0708 --- /dev/null +++ b/README.md @@ -0,0 +1,124 @@ +# laravel-wishlist + +A simple Wishlist implementation for Laravel 5.*.*. + + +## Installation + +First, you'll need to install the package via Composer: + +```shell +$ composer require bhavinjr/laravel-wishlist +``` + +If you are don't use using Laravel 5.5.* Then, update `config/app.php` by adding an entry for the service provider. + + +```php +'providers' => [ + // ... + Bhavinjr\Wishlist\Providers\WishlistServiceProvider::class, +]; + +'aliases' => [ + //... + "Wishlist": "Bhavinjr\Wishlist\Facades\Wishlist", +]; +``` + +In command line paste this command: +```shell +php artisan config:cache +``` + +In command line again, publish the default configuration file: +```shell +php artisan vendor:publish --provider="Bhavinjr\Wishlist\Providers\WishlistServiceProvider" +``` + +In command line paste this command: +```shell +php artisan migrate +``` + + +## Configuration + +Configuration was designed to be as flexible. +global configuration can be set in the `config/wishlist.php` file. + + +``` 'App\Models\Product', +]; +``` + +after update `config/wishlist.php` file. +```shell +php artisan migrate +``` + +## Usage + +The package gives you the following methods to use: + +Adding an item to the wishlist is really simple + +you need specify product_id and user_id respectively all parameter are compulsory + +### Wishlist::add() + +```php +Wishlist::add(15, 1); +``` + +### Wishlist::remove() + +To remove an item from the wishlist, specify the wishlist_id. + +```php +Wishlist::remove(2); +``` + +### Wishlist::getUserWishlist() + +To get users all wishlist item, specify the user_id. + +```php +Wishlist::getUserWishlist(1); +``` + +### Wishlist::removeUserWishlist() + +To remove users all wishlist item, specify the user_id. + +```php +Wishlist::removeUserWishlist(1); +``` + + +### Wishlist::count() + +To count users all wishlist item, specify the user_id. + +```php +Wishlist::count(1); +``` + +### Wishlist::getWishlistItem() + +To get particular wishlist item, specify the product_id and user_id respectively + +```php +Wishlist::getWishlistItem(15, 1); +``` + + +You can also load product detail + +```php +$result = Wishlist::getUserWishlist(1)->load('product'); + +or you can also access directly +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e27c17e --- /dev/null +++ b/composer.json @@ -0,0 +1,35 @@ +{ + "name": "bhavinjr/laravel-wishlist", + "description": "wishlist for laravel 5.*.*", + "keywords": [ + "laravel wishlist", "wishlist laravel", "wishlist","ecommerce laravel wishlist", "laravel ecommerce" + ], + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Bhavin Rudani", + "email": "bhavinrudani94@gmail.com" + } + ], + "minimum-stability": "stable", + "require": { + "php": ">=5.6.0", + "illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*" + }, + "autoload": { + "psr-4": { + "Bhavinjr\\Wishlist\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Bhavinjr\\Wishlist\\Providers\\WishlistServiceProvider" + ], + "aliases": { + "Wishlist": "Bhavinjr\\Wishlist\\Facades\\Wishlist" + } + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..5bd41ab --- /dev/null +++ b/composer.lock @@ -0,0 +1,466 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "7d92b381cb8f15c1d3d2262b14a5d5e1", + "content-hash": "d1de48dde72eab223754a9769c96cea8", + "packages": [ + { + "name": "doctrine/inflector", + "version": "v1.2.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/inflector.git", + "reference": "e11d84c6e018beedd929cff5220969a3c6d1d462" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/inflector/zipball/e11d84c6e018beedd929cff5220969a3c6d1d462", + "reference": "e11d84c6e018beedd929cff5220969a3c6d1d462", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Common String Manipulations with regard to casing and singular/plural rules.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "inflection", + "pluralize", + "singularize", + "string" + ], + "time": "2017-07-22 12:18:28" + }, + { + "name": "illuminate/contracts", + "version": "v5.5.17", + "source": { + "type": "git", + "url": "https://github.com/illuminate/contracts.git", + "reference": "d9e269284eba43bd2e9e8d1f1ba12362b00ec096" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/d9e269284eba43bd2e9e8d1f1ba12362b00ec096", + "reference": "d9e269284eba43bd2e9e8d1f1ba12362b00ec096", + "shasum": "" + }, + "require": { + "php": ">=7.0", + "psr/container": "~1.0", + "psr/simple-cache": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Contracts\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Contracts package.", + "homepage": "https://laravel.com", + "time": "2017-09-19 13:09:37" + }, + { + "name": "illuminate/support", + "version": "v5.5.17", + "source": { + "type": "git", + "url": "https://github.com/illuminate/support.git", + "reference": "132b06edaab3808f63943004911d58785f164ab4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/support/zipball/132b06edaab3808f63943004911d58785f164ab4", + "reference": "132b06edaab3808f63943004911d58785f164ab4", + "shasum": "" + }, + "require": { + "doctrine/inflector": "~1.1", + "ext-mbstring": "*", + "illuminate/contracts": "5.5.*", + "nesbot/carbon": "^1.20", + "php": ">=7.0" + }, + "replace": { + "tightenco/collect": "self.version" + }, + "suggest": { + "illuminate/filesystem": "Required to use the composer class (5.2.*).", + "symfony/process": "Required to use the composer class (~3.3).", + "symfony/var-dumper": "Required to use the dd function (~3.3)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Support\\": "" + }, + "files": [ + "helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Support package.", + "homepage": "https://laravel.com", + "time": "2017-10-17 12:18:29" + }, + { + "name": "nesbot/carbon", + "version": "1.22.1", + "source": { + "type": "git", + "url": "https://github.com/briannesbitt/Carbon.git", + "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc", + "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "symfony/translation": "~2.6 || ~3.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~2", + "phpunit/phpunit": "~4.0 || ~5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.23-dev" + } + }, + "autoload": { + "psr-4": { + "Carbon\\": "src/Carbon/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Nesbitt", + "email": "brian@nesbot.com", + "homepage": "http://nesbot.com" + } + ], + "description": "A simple API extension for DateTime.", + "homepage": "http://carbon.nesbot.com", + "keywords": [ + "date", + "datetime", + "time" + ], + "time": "2017-01-16 07:55:07" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14 16:28:37" + }, + { + "name": "psr/simple-cache", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/753fa598e8f3b9966c886fe13f370baa45ef0e24", + "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2017-01-02 13:31:39" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.6.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", + "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2017-10-11 12:05:26" + }, + { + "name": "symfony/translation", + "version": "v3.4.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation.git", + "reference": "e05b0a5996ad7a35ba3a19ffad8b72c9daa64dfa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation/zipball/e05b0a5996ad7a35ba3a19ffad8b72c9daa64dfa", + "reference": "e05b0a5996ad7a35ba3a19ffad8b72c9daa64dfa", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.4", + "symfony/yaml": "<3.4" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0|~4.0", + "symfony/dependency-injection": "~3.4|~4.0", + "symfony/finder": "~2.8|~3.0|~4.0", + "symfony/intl": "^2.8.18|^3.2.5|~4.0", + "symfony/yaml": "~3.4|~4.0" + }, + "suggest": { + "psr/log": "To use logging capability in translator", + "symfony/config": "", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Translation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Translation Component", + "homepage": "https://symfony.com", + "time": "2017-11-27 14:23:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.6.0" + }, + "platform-dev": [] +} diff --git a/config/wishlist.php b/config/wishlist.php new file mode 100644 index 0000000..ab75751 --- /dev/null +++ b/config/wishlist.php @@ -0,0 +1,15 @@ + 'App\Models\Product', + +]; diff --git a/database/migrations/2017_12_11_061154_create_wishlists_table.php b/database/migrations/2017_12_11_061154_create_wishlists_table.php new file mode 100644 index 0000000..5f18942 --- /dev/null +++ b/database/migrations/2017_12_11_061154_create_wishlists_table.php @@ -0,0 +1,24 @@ +increments('id'); + $table->integer('user_id')->unsigned(); + $table->integer('product_id')->unsigned(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('wishlists'); + } +} diff --git a/src/Facades/Wishlist.php b/src/Facades/Wishlist.php new file mode 100644 index 0000000..985bfbf --- /dev/null +++ b/src/Facades/Wishlist.php @@ -0,0 +1,14 @@ +belongsTo(config('wishlist.product_model'),'product_id'); + } + + public function scopeOfUser($query,$user_id) + { + return $query->where('user_id',$user_id); + } + + public function scopeByProduct($query,$product_id) + { + return $query->where('product_id',$product_id); + } +} diff --git a/src/Providers/WishlistServiceProvider.php b/src/Providers/WishlistServiceProvider.php new file mode 100644 index 0000000..abe0ba2 --- /dev/null +++ b/src/Providers/WishlistServiceProvider.php @@ -0,0 +1,36 @@ +publishConfiguration(); + $this->publishMigrations(); + } + public function register() + { + $config = __DIR__ . '/../../config/wishlist.php'; + $this->mergeConfigFrom($config, 'wishlist'); + $this->app->singleton('wishlist', Wishlist::class); + } + public function provides() + { + return ['Wishlist']; + } + public function publishConfiguration() + { + $path = realpath(__DIR__.'/../../config/wishlist.php'); + $this->publishes([$path => config_path('wishlist.php')], 'config'); + } + public function publishMigrations() + { + $this->publishes([ + __DIR__.'/../../database/migrations/' => database_path('/migrations') + ], 'migrations'); + } +} diff --git a/src/Wishlist.php b/src/Wishlist.php new file mode 100644 index 0000000..9e320c1 --- /dev/null +++ b/src/Wishlist.php @@ -0,0 +1,62 @@ +instance = $this->getWishlist(); + } + + protected function getWishlist() + { + $wishlistModel = new WishlistModel; + return $wishlistModel; + } + + public function add($product_id,$user_id) + { + $this->create($product_id,$user_id); + } + + public function getUserWishlist($user_id) + { + return $this->instance->ofUser($user_id)->get(); + } + + public function remove($id) + { + $this->instance->where('id', $id)->first()->delete(); + } + + public function removeUserWishlist($user_id) + { + $this->instance->ofUser($user_id)->delete(); + } + + protected function create($product_id,$user_id) + { + $matchThese = ['product_id' => $product_id,'user_id' => $user_id]; + + $wishlist = WishlistModel::updateOrcreate($matchThese, + $matchThese); + return $wishlist; + } + + public function count($user_id) + { + return $this->instance->ofUser($user_id)->count(); + } + + public function getWishlistItem($product_id,$user_id) + { + return $this->instance->byProduct($product_id) + ->ofUser($user_id)->first(); + } + +} \ No newline at end of file