This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Recursos para Desenvolvimento Complexo

dev-master 2019-11-07 09:38 UTC

This package is auto-updated.

Last update: 2020-06-07 10:58:31 UTC


❤️ This package helps you to add user based boss system to your model.

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License


  • Support actions:
    • Boss
    • Like
    • Bookmark
    • Subscribe
    • Favorite
    • Vote (Upvote & Downvote)



  • PHP 7.0 +
  • Laravel 5.5 +

You can install the package using composer

$ composer require ricardosierra/laravel-boss -vvv

Then add the service provider to config/app.php


Publish the migrations file:

$ php artisan vendor:publish --provider="Boss\BossServiceProvider" --tag="migrations"

As optional if you want to modify the default configuration, you can publish the configuration file:

$ php artisan vendor:publish --provider="Boss\BossServiceProvider" --tag="config"

And create tables:

$ php artisan migrate

Finally, add feature trait into User model:

use Boss\Traits\CanBoss;
use Boss\Traits\CanBeBossed;

class User extends Model
    use CanBoss, CanBeBossed;


Add CanXXX Traits to User model.

use Boss\Traits\CanBoss;
use Boss\Traits\CanLike;
use Boss\Traits\CanFavorite;
use Boss\Traits\CanSubscribe;
use Boss\Traits\CanVote;
use Boss\Traits\CanBookmark;

class User extends Model
    use CanBoss, CanBookmark, CanLike, CanFavorite, CanSubscribe, CanVote;

Add CanBeXXX Trait to target model, such as 'Post' or 'Music' ...:

use Boss\Traits\CanBeLiked;
use Boss\Traits\CanBeFavorited;
use Boss\Traits\CanBeVoted;
use Boss\Traits\CanBeBookmarked;

class Post extends Model
    use CanBeLiked, CanBeFavorited, CanBeVoted, CanBeBookmarked;

All available APIs are listed below.



$user->bossings()->get() // App\User:class





$user->bookmarks()->get() // App\User:class


$object->bookmarkers()->get() // or $object->bookmarkers 



$user->likes()->get() // default object: App\User:class


$object->likers()->get() // or $object->likers
$object->fans()->get() // or $object->fans



$user->favorites()->get() // App\User:class


$object->favoriters()->get() // or $object->favoriters 



$user->subscriptions()->get() // default object: App\User:class


$object->subscribers() // or $object->subscribers 



$user->vote($target) // Vote with 'upvote' for default




All of the above mentioned methods of creating relationships, such as 'boss', 'like', 'unboss', 'unlike', their syntax is as bosss:

boss(array|int|\Illuminate\Database\Eloquent\Model $targets, $class = __CLASS__)

So you can call them like this:

// Id / Id array
$user->boss(1); // targets: 1, $class = App\User
$user->boss(1, App\Post::class); // targets: 1, $class = App\Post
$user->boss([1, 2, 3]); // targets: [1, 2, 3], $class = App\User

// Model
$post = App\Post::find(7);
$user->boss($post); // targets: $post->id, $class = App\Post

// Model array
$posts = App\Post::popular()->get();
$user->boss($posts); // targets: [1, 2, ...], $class = App\Post

Query relations

$bossers = $user->bossers
$bossers = $user->bossers()->where('id', '>', 10)->get()
$bossers = $user->bossers()->orderByDesc('id')->get()

The other is the same usage.

Working with model.

use Boss\BossRelation;

// get most popular object

// all types
$relations = BossRelation::popular()->get();

// bossable_type = App\Post
$relations = BossRelation::popular(App\Post::class)->get(); 

// bossable_type = App\User
$relations = BossRelation::popular('user')->get();
// bossable_type = App\Post
$relations = BossRelation::popular('post')->get();

// Pagination
$relations = BossRelation::popular(App\Post::class)->paginate(15); 


  • Boss\RelationAttaching
  • Boss\RelationAttached
  • Boss\RelationDetaching
  • Boss\RelationDetached
  • Boss\RelationToggling
  • Boss\RelationToggled
Event::listen(\Boss\RelationAttached::class, function($event) {
    // $event->causer; 
    // $event->getTargetsCollection(); 
    // $event->getRelationType();

About toggled event.

There has a extra properties for Boss\RelationToggled event.

$event->results; // ['attached' => [1, 2, 3], 'detached' => [5, 6]]
$event->attached; // [1, 2, 3]
$event->detached; // [5, 6]

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》