Laravel 4 Blade Form Formatting (Hidden Input)

2019-03-04 17:26发布

I have an "artist" resource, and on the show route page I have a form. I am using Laravel Blade syntax. As part of this form, I am trying to send the page ID to the back end of a Laravel 4 project. I am doing it this way:

{{Form::hidden('artist-id',null, array(
'id' => '{{$artist->id}}',
));}}

However, when using this, the browser throws an error, because it is not rendering the $artist->id variable within the brackets before the outside brackets of the input form.

Is there a way around this or another way to pass back this variable for the resource? Thank you!

2条回答
唯我独甜
2楼-- · 2019-03-04 18:08

You don't need to nest your blade call like that, this should work:

{{ Form::hidden('artist-id', null, array('id' => $artist->id)) }}
查看更多
做自己的国王
3楼-- · 2019-03-04 18:13

So anything inside the blade tags {{ }} is treated as standard PHP. Using further {{ }} inside existing blade tags is just going to throw a big error.

Because anything inside the blade tags is treated as PHP you can simply do the following

{{ Form::hidden('artist-id', null, ['id' => $artist->id]) }}

Although posting that input isn't going to give you the value you want, because the value supplied is null. The id attribute is the html ID given to the html attribute. You need the following to set the value on the input, which will then be posted in with your form data

{{ Form::hidden('artist-id', $artist->id) }}
查看更多
登录 后发表回答