Rikudo Pain
Rikudo Pain

Reputation: 461

Translate code to laravel blade

My condition in view blade cause I ussually used native php coding :

$jlhkriteria = Kriteria::count();

kriterias table : id_kriteria, kriteria_name

@for ($i = 1; $i <= $jlhkriteria; $i ++)
    <?php $queri1 = mysqli_query($mysqli, "SELECT * FROM kriterias WHERE id_kriteria='$i' ORDER BY id_kriteria ASC");
    $kriteria1 = mysqli_fetch_array($queri1, MYSQLI_ASSOC); ?>

    @for ($j = $i + 1; $j <= $jlhkriteria ; $j ++)

    <?php $queri2 = mysqli_query($mysqli, "SELECT * FROM kriterias WHERE id_kriteria='$j' ORDER BY id_kriteria ASC");
            $kriteria2 = mysqli_fetch_array($queri2, MYSQLI_ASSOC); ?>



    <?php echo $i . $j; ?>

<?php echo $kriteria1['kriteria_name']; ?>
<?php echo $kriteria2['kriteria_name']; ?>
@endfor
@endfor

In Laravel 5 I used this code because I think query in View are bad ideas, any ideas to make this in MVC like without query it in Blade view.

Upvotes: 0

Views: 240

Answers (1)

salamas
salamas

Reputation: 515

You will need to create model and controller classes that contain your business logic then pass the data to a view. Something similar to that:

Model:

class Kriteria extends Model
{
    protected $table = 'kriterias';
}

Controller:

class KriteriaController extends Controller
{
    public function index()
    {
        $kriteria = Kriteria::orderBy('id_kriteria')->get();
        return view('index', compact('kriteria'));
    }
}

View:

@for($i=0; $i < count($kriteria); $i ++)
    @for($j = $i + 1; $j < count($kriteria) ; $j ++)
        {{ $i . $j }}
        {{$kriteria[$i]->kriteria_name.' '.$kriteria[$j]->kriteria_name}}
    @endfor
@endfor

Upvotes: 1

Related Questions