LaravelPackages.net
Acme Inc.
Toggle sidebar
tobischulz/laravel-favoritable

Adds an easy way to make every model favoritable

4.726
5
1.4.1
About tobischulz/laravel-favoritable

tobischulz/laravel-favoritable is a Laravel package for adds an easy way to make every model favoritable. It currently has 5 GitHub stars and 4.726 downloads on Packagist (latest version 1.4.1). Install it with composer require tobischulz/laravel-favoritable. Discover more Laravel packages by tobischulz or browse all Laravel packages to compare alternatives.

Last updated

Favoritable

Latest Version on Packagist Total Downloads

Adds an easy way to make every model favoritable.

Installation

You can install the package via composer:

composer require tobischulz/laravel-favoritable

Publish all required assets:

php artisan vendor:publish --provider=TobiSchulz\Favoritable\FavoritableServiceProvider

Migrate your database:

php artisan migrate

Preparation

Add trait to eloquent model that you like to be favoritable.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use TobiSchulz\Favoritable\Traits\Favoriteable;

class Post extends Model
{
    use Favoriteable;

    // ...
}

Add trait your user model that will have favorites.

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use TobiSchulz\Favoritable\Traits\HasFavorites;

class User extends Authenticatable
{
    use HasFavorites;

    // ...
}

Usage

<?php

/**
 * API for Favoritable
 */
$post = Post::find(1);

$post->addFavorite();   // adds this post to favorites to current user
$post->removeFavorite();    // remove this post from favorites
$post->toggleFavorite();    // toggle this post as favorite
$post->isFavorited();   // returns boolean for this post favorite state


$post = Post::find(1);
$user = User::find(2);

$post->addFavorite($user);  // adds favorite for this post and a specific user.

/**
 * API for HasFavorites
 */
$user = User::find(1);

$user->favorites(Post::class);  // returns collection of Post::class favorites

Testing

wip

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Star History Chart