jeudi 31 mars 2016

Query in controller but take variable from Blade for loop value Laravel

Problem is I don't want to query inside blade cause it's a bad idea, I have 2 tables:

"kriterias" table (id_kriteria, kriteria_name)
"evaluasis" table (id_food, id_kriteria, grade)

and have model for it:

Kriteria Model
    public function evaluasi()
    {
        return $this->hasMany(Evaluasi::class, id_kriteria, id_kriteria);
    }

Evaluasi Model
    public function kriteria()
    {
        return $this->belongsTo(Kriteria::class, id_kriteria, id_kriteria);
    }  

Controller part (don't wory about "nis" value, cause I get it from request it's integer) :

$jlhkriteria = Kriteria::count();
return view('rank', compact('nis', 'jlhkriteria));  

This is where my problems begin, I want this code to be applied in my blade view:

@for ($i=1;$i<=$jlhkriteria;$i++)
                    <?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); ?>

<?php echo $hasil['kriteria_name']; ?>
<?php echo $hasil['grade']; ?>
<?php echo $i; ?>

@endfor

As far as I know to join select query above using eloquent I use this :

$evaluasi = Kriteria::with(array('evaluasi' => function($query) use ($nis) { 
        $query  ->where('id_food', $nis)
            ->where('id_kriteria',$i)//this $i value are get from for loop in blade
            ->select('id_kriteria', 'grade'); 
    }))->get();  

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" ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SpKxRU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire