1
votes

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

3

3 Answers

3
votes

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

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