将代码翻译成laravel blade

时间:2016-03-21 13:31:54

标签: laravel-5 blade

我在视图刀片中的条件导致我通常使用本机php编码:

  

$ jlhkriteria = Kriteria :: count();

     

kriterias表: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

在Laravel 5中我使用了这段代码,因为我认为View中的查询是不好的想法,任何想法都可以在MVC中进行,而不在Blade视图中查询。

1 个答案:

答案 0 :(得分:1)

您需要创建包含业务逻辑的模型和控制器类,然后将数据传递给视图。类似的东西:

型号:

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

控制器:

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

查看:

@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