简体   繁体   English

如何在blade.php中的if语句中分配变量

[英]how to assign variable into if statment in blade.php

        @foreach ($modules as $module)

                @if ($module->name === "h_car_positions")   $module_name == "history_car_positions";    @endif


            <tr>
                <td>{{ $module->id }}</td>
                <td><a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id) }}">{{ $module->name }}</a></td>
                <td>{{ $module->name_db }}</td>

                <td>{{ Module::itemCount( $module_name ) }}</td>
                <td>
                    <a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id)}}#fields" class="btn btn-primary btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-edit"></i></a>
                    <a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id)}}#access" class="btn btn-warning btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-key"></i></a>
                    <a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id)}}#sort" class="btn btn-success btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-sort"></i></a>
                    <a module_name="{{ $module->name }}" module_id="{{ $module->id }}" class="btn btn-danger btn-xs delete_module" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-trash"></i></a>
                </td>
            </tr>
        @endforeach

i want to assign history_car_positions to $module name when the if statment is true how to do that in blade 如果if语句为true,我想将history_car_positions分配给$ module名称,如何在刀片中执行此操作

I should first note that your syntax is incorrect - you're comparing ( == ), not assigning ( = ). 首先,请注意您的语法不正确-您正在比较( == ),而不是分配( = )。

But Blade, being an interpreter, has its own control structure for raw PHP. 但是Blade作为解释器,对于原始PHP具有自己的控制结构

However, if you're simply passing this variable into itemCount , you may want to evaluate and assign this in your view, rather than directly in the template. 但是,如果只是将此变量传递给itemCount ,则可能需要在视图中而不是直接在模板中求值并赋值。 It'll be much cleaner for you to maintain. 维护起来会干净得多。

You could try the php directive laravel docs 您可以尝试php指令laravel docs

@if ($module->name === "h_car_positions")
   @php
      $module_name = "history_car_positions"; // use = instead of ==
   @endphp
@endif

使用php标签

@if ($module->name === "h_car_positions")  <?php $module_name = "history_car_positions"; ?> @endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM