Alun

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

Posted 9 months 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:

    -table_a
        -id_a
        -col1
        -col2
        -colN
    -table_b
        -id_b
        -col3
        -col4
        -colM
    -table_pivot
        -id_pivot
        -id_a
        -id_b
        -pivot_1
        -pivot_2
        -pivot_N

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?

Thanks

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

Reply to

Use Markdown with GitHub-flavored code blocks.