I think you can simply achieve this via relationships. Correct me if I am wrong.
Route::with('orders.products')
->where('route_id', $id)->firstOrFail();
Recommendation: https://laravel.com/docs/9.x/eloquent-relationships
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I'm fresh laravel developer and have problem with querying inner relation data.
I want to get route with orders and products for this route, same order sometimes can be assigned to different route, so order and product have to have route_id and i need to have two orders for different routes.
Structure of db looks like this:
routes
orders
products
Relations:
I tried with this query which return route with orders and products, but doesn't use where 'products.route_id':
$id = 105;
Route::with('orders.products')
->whereHas('orders.products', function($q) use ($request) {
$q->where('products.route_id', $id);
})
->where('route_id', $id)->firstOrFail();
#Result from toSql()
select
*
from
`routes`
where
exists (
select
*
from
`orders`
where
`routes`.`route_id` = `orders`.`route_id`
and exists (
select
*
from
`products`
where
`orders`.`order_id` = `products`.`order_id`
and `products`.`route_id` = 105
)
)
and `route_id` = 105
Am i missing something, maybe using wrong relation type? Any help will be appreciated
@przemokon So the Order model connects with the Product model with 2 columns (order_id & route_id) not 1?
In this case you cannot use regular relations, you should use this package https://github.com/topclaudy/compoships which allows you to make relations with more than one columns
EDIT: code sample
Order Model
use Awobaz\Compoships\Compoships;
use Awobaz\Compoships\Database\Eloquent\Relations\HasMany as ComposhipHasMany;
class Order extends Model
{
use Compoships;
public function products(): ComposhipHasMany
{
return $this->hasMany(Product::class, ['item_id', 'route_id'], ['item_id', 'route_id']);
}
}
Product Model
use Awobaz\Compoships\Compoships;
use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo as ComposhipBelongsTo
class Product extends Model
{
public function order(): ComposhipBelongsTo
{
return $this->belongsTo(Order::class, [order_id', 'route_id'], ['order_id', 'route_id']);
}
}
Eager Load
Route::with('orders.products')->where('route_id', $id)->firstOrFail();
Please or to participate in this conversation.