title: Having a Laravel query with a 'join' and a 'where' in order to get a recipe based on allergen constraints?
body: <p>I'm experimenting with Laravel 8.0x Eloquent and Query builder in order to generate a meal plan with recipes chosen by user input constraints. For example, the user inputs they are looking for <strong>Dinner</strong> (meal_type), <strong>Vegetarian</strong> (suitable_for), <strong>6 people</strong> (feeds), for a meal plan of <strong>2 days</strong> (days). The recipe ID is then pulled from the Database based on these constraints.
  The input form is all drop down, except for allergens is which a checkbox (as users may have multiple allergens).</p>
  <p>When it comes to allergens though, one recipe can have multiple allergens. There are two tables, Recipe and Recipe_Allergens. Recipe_Allergens has fields <em>id, Recipe_ID, and Allergen_Description.</em> The checkbox request requires an implode:</p>
  <pre><code>$request-&gt;merge([ 'allergens' =&gt; implode(',', (array) $request-&gt;get('allergens')) ]);
  </code></pre>
  <p>So if two allergens, 'Dairy' and 'Gluten' are checked, the result is saved as 'Dairy,Gluten'. However, I want it to loop through each individual allergen to check if the Recipe contains any of these allergens, and avoid selecting that Recipe_ID if it does.</p>
  <p>The user inputs:</p>
  <pre><code>$meal = $request-&gt;meal_type;
  $suited = $request-&gt;suitable_for;
  $allerg = $request-&gt;allergens;
  $feeds = $request-&gt;no_of_people;
  $days = $request-&gt;no_of_days;
  
  $allergenarray2 = explode(&quot;,&quot;, $allerg); 
  </code></pre>
  <p>The Eloquent 'join' by the Recipe id and 'where' in order to get the Recipe_ID:</p>
  <pre><code>$recipenew = Recipe::join('recipe_allergens', 'recipe_allergens.recipe_id', '=', 'recipe.id')-&gt;where('recipe.suitable_for', $suited)-&gt;where('recipe_allergens.allergen_description', '!=', $allergenarray2)-&gt;where('recipe.feeds_total', $feeds)-&gt;get();
  </code></pre>
  <p>Inputting into DB table:</p>
  <pre><code>while ($x &lt; $days){
    $recidnew =  $recipenew[$x]-&gt;id;
  MealPlanDisplay::create([
        'Recipe_ID' =&gt; $recipenew[$x],
        'Day' =&gt;  $recipeday,
        'user_id' =&gt; $currentuserid,
  ]);
  $x = $x + 1;
  }
  </code></pre>
  <p>All of the constraints work except for the allergens. Does this sort of join work? It won't seem to accept the exploded array either, and only recognises the first value in it (for example Dairy,Gluten is only recognised as 'Dairy'). What can I do?</p>