[SOLVED] How to write a where not in query in laravel?


I want to write a Where not in query in laravel. I wrote in sql it’s working fine please help me convert the query to laravel.

This is the query…

FROM `apiaccessauth` 
WHERE `site_name` NOT IN (
                SELECT `site_name` 
                FROM `API_site_access_log` 
                WHERE `created_at` LIKE '%2021-10-15%'


If you want to use Eloquent, it’ll be like below

Eloquent model


namespace App\Modules\Vehicle\Models;

use Illuminate\Database\Eloquent\Model;

class ApiAccessAuth extends Model
    protected $table = 'apiaccessauth';

Your query

ApiAccessAuth::whereNotIn('site_name', function ($query) {
        ->whereDate('created_at', '2021-10-15');

If you want to use query builder, it’ll be like below

use Illuminate\Support\Facades\DB;

    ->whereNotIn('site_name', function ($query) {
            ->whereDate('created_at', '2021-10-15');

Answered By – sithuaung

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published.