Search code examples
phplaravellaravel-5.3laravel-bladedynamic-html

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


Solution

  • 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>