Laravel, How to use where conditions for relation's column?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Mastering Eloquent Relational Queries in Laravel for Complex Scenarios
Body:
Restaurant management applications often involve multiple tables and complex relationships between them. In Laravel, we use the Eloquent ORM to manage these relationships effortlessly. In this blog post, we'll explore ways to handle common cases like filtering based on certain column values or counting restaurants with specific attributes using Eloquent.
First, consider the following models for our restaurant and cuisine tables:
```php
class Restaurant extends Eloquent {
protected $table = 'restaurants';
public function facilities() {
return $this->hasOne('Facilities');
}
}
class Facilities extends Eloquent {
protected $table = 'restaurants_facilities';
public function restaurant() {
return $this->belongsTo('Restaurant');
}
}
// For Cuisine and related models, use similar relationships.
```
Loading restaurants based on wifi status:
To load all restaurants with either wifi set to 1 or 0, you can use Eloquent's where() method in combination with the Eloquent collection object. Here's an example using a query scope for better code organization and reusability:
```php
class Restaurant extends Eloquent {
// ... other code...
public function scopeWithWifiStatus($query, $status = null) {
if (!is_null($status)) {
return $query->where('wifi', '=', $status);
} else {
return $query->where(function ($q) {
$q->orWhere('wifi', '=', 1)
->orWhere('wifi', '=', 0);
});
}
}
}
```
Now, you can load all restaurants with a specific wifi status using the Restaurant model:
```php
$restaurantsWithWifi1 = Restaurant::withWifiStatus(1)->get();
$restaurantsWithWifi0 = Restaurant::withWifiStatus(0)->get();
// Use either of these queries to retrieve restaurants based on your needs.
```
Loading restaurants with a specific cuisine:
To load all restaurants associated with a particular cuisine_id, you can use eager loading and the belongsToMany() relationship. Here's an example using query scopes for better organization:
```php
class Cuisine extends Eloquent {
// ... other code...
public function scopeWithRestaurants($query, $cuisineId) {
return $query->has('restaurants')->whereHas('restaurants', function ($q) use ($cuisineId) {
$q->wherePivot('cuisine_id', '=', $cuisineId);
});
}
}
```
Now, you can load all restaurants associated with a specific cuisine using the Cuisine model:
```php
$cuisineRestaurants = Cuisine::withRestaurants(6)->get();
```
Counting the number of restaurants with a specific column value:
To find the count of restaurants within a particular condition, you can use Eloquent's count() method. Here's an example for both wifi and parking statuses:
```php
$restaurantsWithWifi1 = Restaurant::withWifiStatus(1)->get()->count();
$restaurantsWithParking1 = Restaurant::where('parking', '=', 1)->count();
```
In summary, Eloquent provides various methods to handle complex relationships and data queries efficiently. By combining query scopes and smartly using Eloquent's built-in functions, you can manage restaurant management applications with ease, allowing for quick and practical solutions to filter and count restaurants based on specific column values.