Mike Thrussell
Mike Thrussell

Reputation: 4525

Conditional @if statement for preselected options in Laravel 5 form using Blade templates

In creating an update form using the same template as the create form, I wish to have the previously chosen options selected. Something like this makes sense, but Blade cannot parse it:

{!! Form::select('number', [1, 2, 3, 4, 5] @if (!empty($at)), $at->id @endif) !!}

I use the $at variable to identify if I'm updating or creating a new entry.

I could write 2 separate statements and echo whichever one is relevant (or use a separate template, but I'm trying to keep the code DRY.

Upvotes: 1

Views: 4139

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152950

The code inside blade tags is just normal PHP. That also means you can use a shorthand inline if:

{!! Form::select('number', [1, 2, 3, 4, 5], (empty($at) ? null : $at->id)) !!}

Upvotes: 4

Related Questions