Can't sort many to many
JekaSylar opened this issue · comments
Good afternoon, sorry for my english.
I have no way to filter when the relationship is many to man.
Here in the model Advertising I have a relationship
public function typeservices()
{
return $this->belongsToMany(
Typeservice::class,
'advertising_typeservice',
'advertising_id',
'typeservice_id'
)->withPivot('price');
}
AdvertisingFilter
public function priceMin($min)
{
$result = $this->related('typeservices', function ($query) use ($min) {
return $query->where('typeservice_id', 2)->wherePivot('price', '>=', $min);
});
return $result;
}
And I have an error.
Please help, I've been scratching my head over this problem for two days now.
exception
:
#"Illuminate\Database\QueryException"
file
:
"\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line
:
760
message
:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause' (SQL: select count(*) as aggregate from advertisings
where exists (select * from typeservices
inner join advertising_typeservice
on typeservices
.id
= advertising_typeservice
.typeservice_id
where advertisings
.id
= advertising_typeservice
.advertising_id
and typeservice_id
= 2 and pivot
= price) and active
= 1)"
trace
:
[,…]
Looks like this is a laravel issue but since you're using withPivot
for price, you should be able to query the price
on that relation like:
public function priceMin($min)
{
$result = $this->related('typeservices', function ($query) use ($min) {
return $query->where('typeservice_id', 2)->where('price', '>=', $min);
});
return $result;
}