Dosmukhanbet
3 months ago
471
7
Laravel

Get belongsToMany with condition

Posted 3 months ago by Dosmukhanbet

Hello, Please could anybody help me solve following issue. I have relation belongsToMany between Routes and Points table and I'd like take only that route which match provided array of ids. The below code return routes if one item of array match

class Routes extends Model
{

    public function points()
    {
        return $this->belongsToMany(Point::class)->withPivot('point_order');
    }

}


$routes = Routes::whereHas('points', function ($query) {
    return $query->whereIn('point_id', [2,7] );
})->get();

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