Note that there are some explanatory texts on larger screens.

plurals
  1. POLaravel, How to use where conditions for relation's column?
    primarykey
    data
    text
    <p>I'm using Laravel and having a small problem with Eloquent ORM.. I can get this working simply with SQL query using a JOIN but I can't seem to get it working with Eloquent!</p> <p>This is what I want, I have two tabels. one is 'Restaurants' and other is 'Restaurant_Facilities'. </p> <p>The tables are simple.. and One-To-One relations. like there is a <code>restaurant</code> table with <code>id</code>, <code>name</code>, <code>slug</code>, etc and another table called <code>restaurant_facilities</code> with <code>id</code>, <code>restaurant_id</code>, <code>wifi</code>, <code>parking</code>, etc</p> <p>Now what I want to do is.. load all restaurants which have wifi = 1 or wifi = 0.. How can i do that with Eloquent ? I have tried eager loading, pivot tables, with(), collections() and nothing seems to work!</p> <p>The same problem I have for a Many-To-Many relation for <code>cuisines</code>! I have the same <code>restaurant</code> table and a <code>cuisine</code> table and a <code>restaurant_cuisine_connection</code> table..</p> <p>but how do I load all restaurants inside a specific cuisine using it's ID ?</p> <p>This works.</p> <p><code>Cuisine::find(6)-&gt;restaurants()-&gt;get();</code></p> <p>but I wanna load this from Restaurant:: model not from cuisines.. because I have many conditions chained together.. its for a search and filtering / browse page.</p> <p>Any ideas or ways ? I've been struggling with this for 3 days and still no answer.</p> <p>Example Models :</p> <pre><code>class Restaurant extends Eloquent { protected $table = 'restaurants'; public function facilities() { return $this-&gt;hasOne('Facilities'); } } class Facilities extends Eloquent { protected $table = 'restaurants_facilities'; public function restaurant() { return $this-&gt;belongsTo('Restaurant'); } } </code></pre> <p>PS : This seems to be working.. but this is not Eloquent way right ?</p> <pre><code>Restaurant::leftJoin( 'cuisine_restaurant', 'cuisine_restaurant.restaurant_id', '=', 'restaurants.id' ) -&gt;where('cuisine_id', 16) -&gt;get(); </code></pre> <p>Also what is the best method to find a count of restaurants which have specific column value without another query ? like.. i have to find the total of restaurants which have parking = 1 and wifi = 1 ?</p> <p>Please help on this.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload