PHP code example of protonemedia / laravel-eloquent-where-not

1. Go to this page and download the library: Download protonemedia/laravel-eloquent-where-not 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-where-not example snippets


use ProtoneMedia\LaravelEloquentWhereNot\WhereNot;

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

    // or use a custom method name:
    WhereNot::addMacro('not');
}

Post::whereNot(function ($query) {
    $query->onFrontPage();
})->get();

Post::whereNot('onFrontPage')->get();

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

Post::whereNot(['ofType' => 'announcement'])->get();

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

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

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

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

    public function scopeOnFrontPage($query)
    {
        $query->where('is_public', 1)
            ->where('votes', '>', 100)
            ->has('comments', '>=', 20)
            ->whereHas('user', fn($user) => $user->isAdmin())
            ->whereYear('published_at', date('Y'));
    }
}

$posts = Post::onFrontPage()->get();

$posts = Post::whereNot(function($query) {
    $query->onFrontPage();
})->get();

$posts = Post::whereNot(fn ($query) => $query->onFrontPage())->get();

Post::whereNot(function ($query) {
    $query->published();
});

// is the same as:

Post::whereNot('published');

Post::whereNot(function ($query) {
    $query->ofType('announcement');
});

// is the same as:

Post::whereNot(['ofType' => 'announcement']);