Wallogit.com
2017 © Pedro Peláez
Laravel Package allows app User to Favorite/Unfavorite Eloquent Model instance
Laravel Package. Allows app User to favorite/unfavorite Eloquent Model instance., (*2)
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
, (*3)
composer require mwazovzky/favoritable
/config/app.php.// file config/app.php ... 'providers' => [ ... \MWazovzky\Favoritable\FavoritableServiceProvider::class ... ]; ...
favorites table$ php artisan migrate
use \MWazovzky\Favoritable\Favoritable;
favorite/unfavorite endpoints available for the application via
adding corresponding routes to 'web' routes groupRoute::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.
, (*4)
/resources/assets/js/components/favoritable/Favorite.vue folder:$ php artisan vendor:publish --tag=assets
<favorite> - favorite/unfavorite button
<favorite-vidget> toggles favorite query string attribute.// 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, (*5)
<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., (*6)
<favorite-vidget></favorite-vidget>
Favorite vidget requires no parameters., (*7)