Atnaize
Atnaize

Reputation: 1816

Laravel Query Builder : Where pivot not in

wherePivotIn is mentionend here (under Filtering Relationships Via Intermediate Table Columns) but I can't find anything about the opposite function.

As the wherePivotIn already exists but not the wherePivotNOTIn, I edited this file : vendor/laravel/framework/src/Illuminate/Database/Eloquant/Relations/BelongsToMany.php

And added this function

public function wherePivotNotIn($column, $values, $boolean = 'and', $not = false)
    {
        $this->pivotWhereIns[] = func_get_args();

        return $this->whereNotIn($this->table.'.'.$column, $values, $boolean, $not);
    }

Now the wherePivotNotIn exist and is working. But my question is:

Is it safe to update this file? In case of update, I guess I will lose this...

Upvotes: 2

Views: 1459

Answers (3)

Atnaize
Atnaize

Reputation: 1816

After dinging a bit, I found out that the whereIn method accept more than 2 arguments.

We juste have to use it like that to use a "wherePivotNotIn"

->wherePivotIn($column,$value,'and','NotIn')

No need to declare a new class or using scope!

Upvotes: 2

Prince Lionel N'zi
Prince Lionel N'zi

Reputation: 2588

Never edit what is inside the vendor directory. Create your own method to meet your need, in case it does not exist.

In your case, you can define a Local Query Scope

It will look like:

class Task extends Model
{

    public function scopeWherePivotNotIn($query)
    {
         /**/            
    }

}

$tasks = Task::wherePivotNotIn()->get();

Upvotes: 0

user320487
user320487

Reputation:

Yeah don't update vendor files that's a no no. Instead , create a class that extends BelongsToMany and put your implementation in there. You'll lose your changes as soon as the file updates.

Upvotes: 0

Related Questions