Added NSFW compatibility
This commit is contained in:
29
app/Models/Scopes/NsfwScope.php
Normal file
29
app/Models/Scopes/NsfwScope.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Scopes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
|
||||
class NsfwScope implements Scope
|
||||
{
|
||||
|
||||
private function canNsfw(): bool
|
||||
{
|
||||
if( \Auth::guest() )
|
||||
return false;
|
||||
|
||||
$user = \Auth::user();
|
||||
return $user->nsfw_content === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the scope to a given Eloquent query builder.
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model): void
|
||||
{
|
||||
if( !$this->canNsfw() )
|
||||
$builder->where('nsfw', false );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user