1 year ago

wherePivot using the word 'pivot' instead of the actual table

Posted 1 year ago by Alun

Hi, I'm trying to make a M2M relation using a custom table with extra values. This values need to be queried so I can retrieve the proper relation. It loosk like this:


Now in my model A I'm trying to do this:

public function getByPivot($val){
    return $this 
        ->wherePivot('pivot_a', '<=', $val)
        ->wherePivot('pivot_b', '>=', $val)->get();

public function table_b(){
    return $this->belongsToMany(Table_B::class, 'table_pivot', 'id_a', 'id_b')
        ->withPivot(['pivot_a', 'pivot_b']);

The SQL I'm getting with this is as below:

select * from `table_a` where `pivot` = pivot_a and `pivot` = pivot_b

I don't know if this is an actual bug or I'm doing something wrong, but the SQL I want to achieve is like this:

select * from `table_a` where `pivot_a` <= val and `pivot_b` >= val

How can I?


Please sign in or create an account to participate in this conversation.