Make Eloquent Model models wishlistable
You can install the package via composer:
composer require alpetg/wishlist
You can publish and run the migrations with:
php artisan vendor:publish --tag="wishlist-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="wishlist-config"
Add Wishlist
on User Model
use AlpetG\Wishlist\Traits\Wishlist;
class User extends Authenticatable
{
use Wishlist;
<...>
}
Add Wishlistable
on every model u want have wishlist on
use AlpetG\Wishlist\Traits\Wishlistable;
class Products extends Model
{
use Wishlistable;
<...>
}
$user = User::find(1);
$product = Product::find(3);
$user->wish($product);
$user->unwish($product);
$user->toggleWishlist($product);
//Or Dircet from product
$product->wish()
$product->unwish()
$product->toggleWishlist()
// All
$user->->wishlist()->count();
//or
$user->wishlistCount();
// with type
$user->wishlist()->withType(Product::class)->count();
$user = User::withCount('wishlist')->get();
foreach ($user_count as $user) {
echo $user->wishlist_count;
}
You can use Wishlist::attachWishlistStatus(Collection $wishlists)
to attach the user Wishlist status, it will set is_wishlisted
attribute to each model of $wishlists
:
$user1 = User::find(1);
$user->attachWishlistStatus($user1);
// result
[
"id" => 1
"name" => "user1"
"created_at" => "2022-09-24T23:06:47.000000Z"
"updated_at" => "2022-09-24T23:06:47.000000Z"
"is_wishlisted" => true
]
$user = auth()->user();
$products = Product::oldest('id')->get();
$products = $user->attachWishlistStatus($products);
$products = $products->toArray();
// result
[
[
"id" => 1
"title" => "product 1"
"created_at" => "2022-09-24T23:06:47.000000Z"
"updated_at" => "2022-09-24T23:06:47.000000Z"
"is_wishlisted" => true
],
[
"id" => 2
"title" => "product 2"
"created_at" => "2022-09-24T23:06:47.000000Z"
"updated_at" => "2022-09-24T23:06:47.000000Z"
"is_wishlisted" => true
],
[
"id" => 3
"title" => "product 3"
"created_at" => "2022-09-24T23:06:47.000000Z"
"updated_at" => "2022-09-24T23:06:47.000000Z"
"is_wishlisted" => false
],
]
$products = Product::paginate(10);
$user->attachWishlistStatus($products);
$products = Product::paginate(10);
$user->getMyWish($products);
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.