PHP code example of dmdboi / laravel-autoscopes

1. Go to this page and download the library: Download dmdboi/laravel-autoscopes 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/ */

    

dmdboi / laravel-autoscopes example snippets


class Post extends Model {
    protected $fillable = ['user_id', 'title', 'content'];
}

class Comment extends Model {
    protected $fillable = ['user_id', 'post_id', 'content'];
}



declare(strict_types=1);

namespace App\Models\Traits;

use Illuminate\Database\Eloquent\Builder;

trait HasUserScopes
{
    /**
     * @param Builder<\Illuminate\Database\Eloquent\Model> $query
     * @param int|string $userId
     * @return Builder<\Illuminate\Database\Eloquent\Model>
     */
    public function scopeForUser(Builder $query, int|string $userId): Builder
    {
        return $query->where('user_id', $userId);
    }
    
    // scopeForUserIn() and scopeExceptUser() also 
bash
php artisan scopes:generate User
php artisan scopes:generate Post
bash
php artisan scopes:add HasUserScopes
php artisan scopes:add HasPostScopes