在Laravel中,如何在name属性中显示php值?

时间:2019-05-03 12:44:54

标签: php laravel laravel-blade

我在刀片文件中包含以下html代码:

@foreach ($engagements as $engagement)
    {!! Form::checkbox('engagements[]', $engagement->id, in_array($engagement->id, $user->privileges->pluck(engagement_id)->toArray()) ? true : false) !!} {{ $engagement->name }}
    {!! Form::select('roles[]', $userRoles, $user->privileges->where(engagement_id, $engagement->id)->first()[role]) !!}
@endforeach`

我想以$engagement->id的身份在roles[]中传递roles[{{@engegement_id}}]

但是,它没有显示{{@engegement_id}}的值,而是在html视图源中将其显示为角色[{{@engegement_id}}]

我在这里做什么错了?

2 个答案:

答案 0 :(得分:0)

尝试一下,

@foreach ($engagements as $engagement)
@php echo $roleWithId = roles[$engagement->id] @endphp
{!! Form::checkbox('engagements[]', $engagement->id, in_array($engagement->id, $user->privileges->pluck(engagement_id)->toArray()) ? true : false) !!} {{ $engagement->name }}
{!! Form::select($roleWithId , $userRoles, $user->privileges->where(engagement_id, $engagement->id)->first()[role]) !!}
@endforeach

制作要传递的字符串并将其存储在变量中,然后在Form select中传递该变量。

希望这会有所帮助:)

答案 1 :(得分:0)

我们不能这样,它给@php echo $roleWithId = roles[$engagement->id] @endphp说了“未定义的变量角色”错误。但是,我在选择本身时尝试了

{!! Form::select('roles[@php echo $engagement->id @endphp]', $userRoles, $user->privileges->where(\App\Privilege::COLUMN_ENGAGEMENT_ID, $engagement-> {\App\Engagement::COLUMN_ID})->first()[\App\Privilege::COLUMN_ROLE]) !!}

但是,它显示为

<select name="roles[<?php echo $engagement->id ?>]">在视图源中