All Projects → overtrue → laravel-favorite

overtrue / laravel-favorite

Licence: other
❤️ User favorite feature for Laravel Application.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-favorite

SocialMediaAppForFoodies
This is a Social networking android app for food lovers. It is a way to connect with other foodies and grow your network of friends and followers. In this app we can, 1.Users can create their own account and maintain it. 2.Post the Recipes with image and Description. 3.Followers can like and Comment on your post 4.Home screen with your and whom …
Stars: ✭ 40 (-78.14%)
Mutual labels:  followers, following
github-unfollower-detector
💀 Small application to detect evil users who have stopped following you on Github 💀
Stars: ✭ 42 (-77.05%)
Mutual labels:  followers
goodreads-toolbox
9 tools for Goodreads.com, for finding people based on the books they’ve read, finding books popular among the people you follow, following new book reviews, etc
Stars: ✭ 56 (-69.4%)
Mutual labels:  followers
meet-the-fans
Query and Visualize the network graph of your GitHub repositories, followers, stargazers, and forks.
Stars: ✭ 22 (-87.98%)
Mutual labels:  followers
bot
Completely free and open-source human-like Instagram bot. Powered by UIAutomator2 and compatible with basically any Android device 5.0+ that can run Instagram - real or emulated.
Stars: ✭ 321 (+75.41%)
Mutual labels:  followers
Insta-Bot
Python bot using Selenium increasing Instagram Followers.
Stars: ✭ 62 (-66.12%)
Mutual labels:  followers
WatchSomething
Project that uses an API to list movies and tv shows that are latest, popular, top rated & on air.
Stars: ✭ 11 (-93.99%)
Mutual labels:  favorites
ouuan
A profile README with 86 stars and 152 forks 🌟
Stars: ✭ 86 (-53.01%)
Mutual labels:  followers
Arduino-OpenCV-Human-Follower
Face detector and follower using Arduino and OpenCV in Python
Stars: ✭ 30 (-83.61%)
Mutual labels:  followers
uuid
A decentralized favorites and bookmarks based on Git hosting
Stars: ✭ 70 (-61.75%)
Mutual labels:  favorites
BeRude
A fun tool that lists all users who don't follow you back on GitHub😁
Stars: ✭ 33 (-81.97%)
Mutual labels:  followers
mikaela
Mikaela is a discord music bot that gives users the ability to store their favorite songs, and create playlists on discord.
Stars: ✭ 19 (-89.62%)
Mutual labels:  favorites
mastodon-autofollow
Autofollow bot for mastodon
Stars: ✭ 28 (-84.7%)
Mutual labels:  followers
laravel-follow
Laravel Follow System for Eloquent models.
Stars: ✭ 45 (-75.41%)
Mutual labels:  followers
booster
twitter bot that builds followers
Stars: ✭ 30 (-83.61%)
Mutual labels:  followers
Mobile Web Favorites
This is a favorites, with a mobile web tips.
Stars: ✭ 1,724 (+842.08%)
Mutual labels:  favorites
Favorites Web
云收藏 Spring Boot 2.X 开源项目
Stars: ✭ 4,485 (+2350.82%)
Mutual labels:  favorites

Laravel Favorite

❤️ User favorite feature for Laravel Application.

CI Latest Stable Version Latest Unstable Version Total Downloads License

Sponsor me

Installing

$ composer require overtrue/laravel-favorite -vvv

Configuration

This step is optional

$ php artisan vendor:publish --provider="Overtrue\\LaravelFavorite\\FavoriteServiceProvider" --tag=favorite-config

Migrations

This step is also optional, if you want to custom favorites table, you can publish the migration files:

$ php artisan vendor:publish --provider="Overtrue\\LaravelFavorite\\FavoriteServiceProvider" --tag=favorite-migrations

Usage

Traits

Overtrue\LaravelFavorite\Traits\Favoriter

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelFavorite\Traits\Favoriter;

class User extends Authenticatable
{
    use Favoriter;
    
    <...>
}

Overtrue\LaravelFavorite\Traits\Favoriteable

use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelFavorite\Traits\Favoriteable;

class Post extends Model
{
    use Favoriteable;

    <...>
}

API

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

$user->favorite($post);
$user->unfavorite($post);
$user->toggleFavorite($post);
$user->getFavoriteItems(Post::class)

$user->hasFavorited($post); 
$post->hasBeenFavoritedBy($user); 

Get object favoriters:

foreach($post->favoriters as $user) {
    // echo $user->name;
}

Get Favorite Model from User.

Used Favoriter Trait Model can easy to get Favoriteable Models to do what you want. *note: this method will return a Illuminate\Database\Eloquent\Builder *

$user->getFavoriteItems(Post::class);

// Do more
$favortePosts = $user->getFavoriteItems(Post::class)->get();
$favortePosts = $user->getFavoriteItems(Post::class)->paginate();
$favortePosts = $user->getFavoriteItems(Post::class)->where('title', 'Laravel-Favorite')->get();

Aggregations

// all
$user->favorites()->count(); 

// with type
$user->favorites()->withType(Post::class)->count(); 

// favoriters count
$post->favoriters()->count();

List with *_count attribute:

$users = User::withCount('favorites')->get();

foreach($users as $user) {
    echo $user->favorites_count;
}


// for Favoriteable models: 
$posts = Post::withCount('favoriters')->get();

foreach($posts as $post) {
    echo $post->favorites_count;
}

Attach user favorite status to favoriteable collection

You can use Favoriter::attachFavoriteStatus($favoriteables) to attach the user favorite status, it will set has_favorited attribute to each model of $favoriteables:

For model

$post = Post::find(1);

$post = $user->attachFavoriteStatus($post);

// result
[
    "id" => 1
    "title" => "Add socialite login support."
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_favorited" => true
 ],

For Collection | Paginator | LengthAwarePaginator | array:

$posts = Post::oldest('id')->get();

$posts = $user->attachFavoriteStatus($posts);

$posts = $posts->toArray();

// result
[
  [
    "id" => 1
    "title" => "Post title1"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_favorited" => true
  ],
  [
    "id" => 2
    "title" => "Post title2"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_favorited" => fasle
  ],
  [
    "id" => 3
    "title" => "Post title3"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_favorited" => true
  ],
]

For pagination

$posts = Post::paginate(20);

$user->attachFavoriteStatus($posts);

N+1 issue

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:

// Favoriter
$users = User::with('favorites')->get();

foreach($users as $user) {
    $user->hasFavorited($post);
}

// Favoriteable
$posts = Post::with('favorites')->get();
// or 
$posts = Post::with('favoriters')->get();

foreach($posts as $post) {
    $post->isFavoritedBy($user);
}

Events

Event Description
Overtrue\LaravelFavorite\Events\Favorited Triggered when the relationship is created.
Overtrue\LaravelFavorite\Events\Unfavorited Triggered when the relationship is deleted.

Related packages

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

❤️ Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

PHP 扩展包开发

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

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

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].