Yeasir Arafat Majumder
Yeasir Arafat Majumder

Reputation: 1264

render dynamic anchor tag in laravel 5.3 blade

I am learning laravel for last couple of days. Can not seem to figure out how to render dynamic anchor tag inside a ternary operator. Below is my code snippet:

@foreach($task_list as $task)
  <td>{!!  $task->completed? 'Yes' : '<a href="/task/complete/$task->id" >Mark as complete</a>' !!}</td>
@endforeach

Basically i am checking if a particular task is complete(via the $task->complete model attribute). If yes then display the string "Yes", otherwise render a link that says "mark as complete". This link will take the user to a route "/task/complete/{id of the task}" where i will process it further.

I am unalbe the get the id of the task to be part of the link url(via the $task->id attribute). Would much appreciate some help and knowledge sharing

Upvotes: 2

Views: 3876

Answers (3)

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

Printing HTML with PHP inside Blade template is not a good idea. I'd recommend you to use simple and readable @if solution instead of ternary operator:

<td>
    @if ($task->completed)
        Yes
    @else
        <a href="{{ url('task/complete/'.$task->id) }}">Mark as complete</a>
    @endif
</td>

Upvotes: 4

Bhaumik Pandhi
Bhaumik Pandhi

Reputation: 2683

You can try like this, but you need to use external package for that, Try this

@foreach($task_list as $task)
  <td>{!!  $task->completed? 'Yes' : link_to('url', $title = null, $attributes = [], $secure = null);
 !!}</td>
@endforeach

Upvotes: 1

eray
eray

Reputation: 81

@foreach($task_list as $task)
   <td>{!!  $task->completed? 'Yes' : '<a href="/task/complete/'.$task->id.'" >Mark as complete</a>' !!}</td>
@endforeach

Upvotes: 1

Related Questions