PHP code example of overtrue / laravel-favorite
1. Go to this page and download the library: Download overtrue/laravel-favorite library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
overtrue / laravel-favorite example snippets
php artisan vendor:publish --provider="Overtrue\LaravelFavorite\FavoriteServiceProvider"
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;
<...>
}
use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelFavorite\Traits\Favoriteable;
class Post extends Model
{
use Favoriteable;
<...>
}
$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);
foreach($post->favoriters as $user) {
// echo $user->name;
}
$user->getFavoriteItems(Post::class);
// Do more
$favoritePosts = $user->getFavoriteItems(Post::class)->get();
$favoritePosts = $user->getFavoriteItems(Post::class)->paginate();
$favoritePosts = $user->getFavoriteItems(Post::class)->where('title', 'Laravel-Favorite')->get();
// all
$user->favorites()->count();
// with type
$user->favorites()->withType(Post::class)->count();
// favoriters count
$post->favoriters()->count();
$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;
}
$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
],
$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" => false
],
[
"id" => 3
"title" => "Post title3"
"created_at" => "2021-05-20T03:26:16.000000Z"
"updated_at" => "2021-05-20T03:26:16.000000Z"
"has_favorited" => true
],
]
$posts = Post::paginate(20);
$user->attachFavoriteStatus($posts);
// Favoriter
$users = User::with('favorites')->get();
foreach($users as $user) {
$user->hasFavorited($post);
}
// with favoriteable object
$users = User::with('favorites.favoriteable')->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);
}