如何将变量赋值给blade.php中的if语句

问题描述 投票:-1回答:3
        @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

我想在if语句为true时将history_car_positions分配给$ module name如何在blade中执行此操作

php laravel-5 laravel-blade laraadmin
3个回答
1
投票

我应该首先注意你的语法不正确 - 你正在比较(==),而不是分配(=)。

但作为一名翻译,Blade已经为原始PHP提供了its own control structure

但是,如果您只是将此变量传递给itemCount,则可能需要在视图中评估并分配此变量,而不是直接在模板中。维护它会更加清洁。


0
投票

你可以试试php指令laravel docs

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

0
投票

使用php标签

@if ($module->name === "h_car_positions")  <?php $module_name = "history_car_positions"; ?> @endif
© www.soinside.com 2019 - 2024. All rights reserved.