title: Separate and convert native query from blade to eloquent laravel
body: <p>Problem is I don't want to query inside blade cause it's a bad idea, I have 2 tables:  </p>
  
  <blockquote>
    <p>"kriterias" table (id_kriteria, kriteria_name)<br>
    "evaluasis" table (id_food, id_kriteria, grade)</p>
  </blockquote>
  
  <p>This is where my problems begin, I want this code to be applied in my blade view:  </p>
  
  <pre><code>@for ($i=1;$i&lt;=$jlhkriteria;$i++)
                      &lt;?php $evaluasi = mysqli_query($mysqli, "SELECT k.kriteria_name,e.id_kriteria,e.grade FROM evaluasis e, kriterias k WHERE
                  e.id_food='$nis' AND e.id_kriteria='$i' AND e.id_kriteria=k.id_kriteria");
                      $hasil = mysqli_fetch_array($evaluasi, MYSQLI_ASSOC); ?&gt;
  
  &lt;?php echo $hasil['kriteria_name']; ?&gt;
  &lt;?php echo $hasil['grade']; ?&gt;
  &lt;?php echo $i; ?&gt;
  
  @endfor
  </code></pre>
  
  <blockquote>
    <p>As far as I know to join select query above using eloquent I use this
    :  </p>
  </blockquote>
  
  <pre><code>$evaluasi = Kriteria::with(array('evaluasi' =&gt; function($query) use ($nis) { 
          $query  -&gt;where('id_food', $nis)
              -&gt;where('id_kriteria',$i)//this $i value are get from for loop in blade
              -&gt;select('id_kriteria', 'grade'); 
      }))-&gt;firstOrFail();  
  </code></pre>
  
  <p>I always have problem when query something inside for loop in blade. My previous try are do the for loop in controller and foreach it in blade, for the $i value i get id_kriteria value, but I want to know how to query it in controller while taking $i value in blade and echo "kriteria_name" and "grade" and "$i" ?  </p>
  
  <p><strong>EDIT 1:</strong><br>
  add Model and controller
  and have model for it:  </p>
  
  <pre><code>Kriteria Model
      public function evaluasi()
      {
          return $this-&gt;hasMany(Evaluasi::class, id_kriteria, id_kriteria);
      }
  
  Evaluasi Model
      public function kriteria()
      {
          return $this-&gt;belongsTo(Kriteria::class, id_kriteria, id_kriteria);
      }  
  </code></pre>
  
  <p>Controller part (don't wory about "nis" value, cause I get it from request it's integer) :</p>
  
  <pre><code>$jlhkriteria = Kriteria::count();
  return view('rank', compact('nis', 'jlhkriteria));  
  </code></pre>