Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

guillaumebriday/laravel-blog

1797 stars
2 code files
View guillaumebriday/laravel-blog on GitHub

master/app/Scopes/PostedScope.php

Open in GitHub
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\Auth;
 
class PostedScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$user = Auth::user() ?? Auth::guard('api')->user();
 
if (!$user || !$user->isAdmin()) {
$builder->where('posted_at', '<=', now());
}
}
}

app/Models/Post.php

Open in GitHub
use App\Scopes\PostedScope;
use Illuminate\Database\Eloquent\Model;
 
class Post extends Model
{
protected static function boot(): void
{
parent::boot();
static::addGlobalScope(new PostedScope);
}
{

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.