1. Go to this page and download the library: Download sowailem/flagable 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/ */
sowailem / flagable example snippets
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Sowailem\Flagable\Traits\CanFlag;
use Sowailem\Flagable\Traits\Flagable;
// User model (can flag other models)
class User extends Authenticatable
{
use CanFlag;
// Your existing model code...
}
// Post model (can be flagged by other models)
class Post extends Model
{
use Flagable;
// Your existing model code...
}
// Comment model (can both flag and be flagged)
class Comment extends Model
{
use CanFlag, Flagable;
// Your existing model code...
}
$user = User::find(1);
$post = Post::find(1);
// Flag a post
$flag = $user->flag($post, 'like');
// Check if user has flagged the post
if ($user->hasFlagged($post, 'like')) {
echo "User likes this post!";
}
// Unflag a post
$user->unflag($post, 'like');
// Get flag count for a post
$likeCount = $post->flagCount('like');
$totalFlags = $post->flagCount(); // All flag types
// Get all users who liked a post
$likers = $post->flaggers('like', User::class);
// Check if post is flagged by a specific user
$isLiked = $post->isFlaggedBy($user, 'like');
use Sowailem\Flagable\Traits\CanFlag;
class User extends Model
{
use CanFlag;
}
use Sowailem\Flagable\Traits\Flagable;
class Post extends Model
{
use Flagable;
}
use Sowailem\Flagable\Facades\Flag;
// Add custom flag types
Flag::addFlagType('report');
Flag::addFlagType('spam');
Flag::addFlagType('inappropriate');
// Use them immediately
$user->flag($post, 'report');
// Flag multiple posts
$posts = Post::whereIn('id', [1, 2, 3])->get();
foreach ($posts as $post) {
$user->flag($post, 'like');
}
// Get flag counts for multiple posts
$posts = Post::with(['flags' => function ($query) {
$query->whereHas('link.type', function ($q) {
$q->where('name', 'like');
});
}])->get();
foreach ($posts as $post) {
$likeCount = $post->flagCount('like');
echo "Post {$post->id} has {$likeCount} likes\n";
}
// Get posts with more than 10 likes
$popularPosts = Post::whereHas('flags', function ($query) {
$query->whereHas('link.type', function ($q) {
$q->where('name', 'like');
});
}, '>', 10)->get();
// Get users who liked specific posts
$postIds = [1, 2, 3];
$likers = User::whereHas('flags', function ($query) use ($postIds) {
$query->whereHas('link', function ($q) use ($postIds) {
$q->whereHas('target', function ($target) {
$target->where('name', Post::class);
})->whereHas('type', function ($type) {
$type->where('name', 'like');
});
})->whereIn('flagger_id', function ($subQuery) use ($postIds) {
$subQuery->select('id')->from('posts')->whereIn('id', $postIds);
});
})->get();
// In your User model
public function likedPosts()
{
return $this->belongsToMany(Post::class, 'flags', 'flagger_id', 'flagger_id')
->whereHas('flags.link.type', function ($query) {
$query->where('name', 'like');
})
->where('flagger_type', User::class);
}
// In your Post model
public function likers()
{
return $this->belongsToMany(User::class, 'flags', 'flagger_id', 'flagger_id')
->whereHas('flags.link.type', function ($query) {
$query->where('name', 'like');
})
->where('flagger_type', User::class);
}
// All default types are immediately available
$user->flag($post, 'like');
$user->flag($anotherUser, 'follow');
$user->flag($post, 'bookmark');
$user->flag($comment, 'upvote');
// Efficient: Use specific flag type
$likeCount = $post->flagCount('like');
// Less efficient: Count all flags then filter
$allFlags = $post->flags()->get();
$likeCount = $allFlags->where('link.type.name', 'like')->count();
// Efficient: Eager load relationships
$posts = Post::with(['flags.link.type'])->get();
// Efficient: Use database aggregation
$popularPosts = Post::withCount(['flags' => function ($query) {
$query->whereHas('link.type', function ($q) {
$q->where('name', 'like');
});
}])->having('flags_count', '>', 10)->get();
// Cache flag counts
$cacheKey = "post_{$post->id}_like_count";
$likeCount = Cache::remember($cacheKey, 3600, function () use ($post) {
return $post->flagCount('like');
});
// Cache popular posts
$popularPosts = Cache::remember('popular_posts', 1800, function () {
return Post::withCount(['flags' => function ($query) {
$query->whereHas('link.type', function ($q) {
$q->where('name', 'like');
});
}])->orderBy('flags_count', 'desc')->take(10)->get();
});
if (!$user->hasFlagged($post, 'like')) {
$user->flag($post, 'like');
}
// Instead of this:
foreach ($posts as $post) {
$post->flagCount('like'); // N+1 query problem
}
// Do this:
$posts = Post::withCount(['flags' => function ($query) {
$query->whereHas('link.type', function ($q) {
$q->where('name', 'like');
});
}])->get();
foreach ($posts as $post) {
echo $post->flags_count; // No additional queries
}
Post::chunk(100, function ($posts) {
foreach ($posts as $post) {
// Process each post
$likeCount = $post->flagCount('like');
}
});
DB::enableQueryLog();
// Your flagging operations here
$user->flag($post, 'like');
// Check executed queries
$queries = DB::getQueryLog();
dd($queries);
use Sowailem\Flagable\Models\FlagType;
$validTypes = FlagType::pluck('name')->toArray();
if (in_array($requestedType, $validTypes)) {
$user->flag($post, $requestedType);
} else {
throw new InvalidArgumentException("Invalid flag type: {$requestedType}");
}
// Example authorization check
public function flagPost(Request $request, Post $post)
{
$this->authorize('flag', $post);
$request->validate([
'type' => '