PHP code example of protonemedia / laravel-eloquent-scope-as-select

1. Go to this page and download the library: Download protonemedia/laravel-eloquent-scope-as-select 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/ */

    

protonemedia / laravel-eloquent-scope-as-select example snippets


use ProtoneMedia\LaravelEloquentScopeAsSelect\ScopeAsSelect;

public function boot()
{
    ScopeAsSelect::addMacro();

    // or use a custom method name:
    ScopeAsSelect::addMacro('withScopeAsSubQuery');
}

Post::addScopeAsSelect('is_published', function ($query) {
    $query->published();
})->get();

Post::addScopeAsSelect('is_published', 'published')->get();

Post::addScopeAsSelect('is_popular_and_published', ['popular', 'published'])->get();

Post::addScopeAsSelect('is_announcement', ['ofType' => 'announcement'])->get();

Post::addScopeAsSelect('is_announcement', ['publishedBetween' => [2010, 2020]])->get();

Post::addScopeAsSelect('is_published_announcement', [
    'published',
    'ofType' => 'announcement'
])->get();

Post::addScopeAsSelect('is_not_announcement', ['ofType' => 'announcement'], false)->get();

class Post extends Model
{
    public function scopePublished($query)
    {
        return $query->whereNotNull('published_at');
    }
}

$allPublishedPosts = Post::published()->get();

Post::get()->each(function (Post $post) {
    $isPublished = !is_null($post->published_at);
});

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function scopePublished($query)
    {
        return $query->whereNotNull('published_at');
    }

    public function scopePublishedInCurrentYear($query)
    {
        return $query->whereYear('published_at', date('Y'));
    }
}

$recentPopularPosts = Post::query()
    ->publishedInCurrentYear()
    ->has('comments', '>=', 10)
    ->get();

Post::get()->each(function (Post $post) {
    $isRecentAndPopular = $post->comments()->count() >= 10
        && optional($post->published_at)->isCurrentYear();
});

$posts = Post::addScopeAsSelect('is_published', function ($query) {
    $query->published();
})->get();

$posts = Post::addScopeAsSelect('is_published', fn ($query) => $query->published())->get();

$posts->each(function (Post $post) {
    $isPublished = $post->is_published;
});

Post::query()
    ->addScopeAsSelect('is_published', function ($query) {
        $query->published();
    })
    ->addScopeAsSelect('is_recent_and_popular', function ($query) {
        $query->publishedInCurrentYear()->has('comments', '>=', 10);
    })
    ->get()
    ->each(function (Post $post) {
        $isPublished = $post->is_published;

        $isRecentAndPopular = $post->is_recent_and_popular;
    });

Post::addScopeAsSelect('is_published', function ($query) {
    $query->published();
});

// is the same as:

Post::addScopeAsSelect('is_published', 'published');

Post::addScopeAsSelect('is_announcement', function ($query) {
    $query->ofType('announcement');
});

// is the same as:

Post::addScopeAsSelect('is_announcement', ['ofType' => 'announcement']);

$postA = Post::addScopeAsSelect('is_announcement', ['ofType' => 'announcement'])->first();
$postB = Post::addScopeAsSelect('is_not_announcement', ['ofType' => 'announcement'], false)->first();

$this->assertTrue($postA->is_announcement)
$this->assertFalse($postB->is_not_announcement);