mwazovzky/favoritable

Laravel Package allows app User to Favorite/Unfavorite Eloquent Model instance

v0.0.5 2017-11-09 15:44 UTC

This package is auto-updated.

Last update: 2024-04-10 16:53:22 UTC


README

Build Status Coverage Status

68747470733a2f2f6c61726176656c2e636f6d2f6173736574732f696d672f636f6d706f6e656e74732f6c6f676f2d6c61726176656c2e737667

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.

  1. Pull the package into Laravel project
composer require mwazovzky/favoritable
  1. 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
...
];
...
  1. Run database migration to create favorites table
$ php artisan migrate
  1. 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;
  1. Package makes favorite/unfavorite endpoints 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.

  1. Run artisan command to publish package assets to /resources/assets/js/components/favoritable/Favorite.vue folder:
$ php artisan vendor:publish --tag=assets
  1. Published vue component are: <favorite> - favorite/unfavorite button <favorite-vidget> toggles favorite query string attribute.
  2. 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.