mwazovzky / favoritable
Laravel Package allows app User to Favorite/Unfavorite Eloquent Model instance
Installs: 38
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mwazovzky/favoritable
This package is auto-updated.
Last update: 2025-11-10 20:13:43 UTC
README
Project: mwazovzky\favoritable
Description
Laravel Package. Allows app User to favorite/unfavorite Eloquent Model instance.
Version: 0.0.6
Change log:
0.0.6 frontend assets added: <favorite-vidget> vue component
0.0.5 frontend assets added: <favorite> vue component and favorite vidget (blade partial)
0.0.4 routes and controller to favorite/unfavorite model added
0.0.3 package auto discovery (as of Laravel 5.5)
0.0.2 added Model::favoritedBy() methods that define Many-To-Many Polymorphic Relationships
0.0.1 initial project scaffolding
Installation.
- Pull the package into Laravel project
composer require mwazovzky/favoritable
- For Laravel 5.4 or below register package service provider at
/config/app.php.
Package will be auto-registered for Laravel 5.5 and above.
// file config/app.php
...
'providers' => [
...
\MWazovzky\Favoritable\FavoritableServiceProvider::class
...
];
...
- Run database migration to create
favoritestable
$ php artisan migrate
- Use trait Favoritable for every Model that can be favorited by a User.
Check trait docblocks for a list of available methods.
use \MWazovzky\Favoritable\Favoritable;
- Package makes
favorite/unfavoriteendpoints available for the application via adding corresponding routes to 'web' routes group
Route::post('/favorites/{model}/{id}', 'FavoritesController@store')->name('favorites.store');
Route::delete('/favorites/{model}/{id}', 'FavoritesController@destroy')->name('favorites.destroy');
where model and id are short model class name (kebab-case for KebabCase) and
id for the model being favorited/unfavorited.
- Run artisan command to publish package assets to
/resources/assets/js/components/favoritable/Favorite.vuefolder:
$ php artisan vendor:publish --tag=assets
- Published vue component are:
<favorite>- favorite/unfavorite button<favorite-vidget>togglesfavoritequery string attribute. - Register components:
// file /resources/assets/js/app.js
Vue.component('favorite', require('./components/favoritable/Favorite.vue'));
Vue.component('favorite-vidget', require('./components/favoritable/FavoriteVidget.vue'));
Component usage
<favorite type="modelClass" :model={{ $model->favoriteAttributes() }}></favorite>
where
modelClass is a short model class name (use kebab-case for KebabCase),
$model is a model instance,
Model::favoriteAttributes() is a method provided by Favoritable trait.
Any object (e.g. model itself) that has: id, isFavoreted and favoritesCount
fields may be passed as component model property.
<favorite-vidget></favorite-vidget>
Favorite vidget requires no parameters.