php 如何在Laravel应用程序中使用我刀片文件中的if else条件?

agxfikkp  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(135)

我需要在我的laravel blade文件中使用if else条件。打印不同的图标。这是我当前的刀片文件

@foreach ($project->tasks as $task)
  <ul>
    <li>
  <div>
  <div class="pull-right icons-align">
            <a href="{{url('projects/'.$project->id.'/task/'.$task->id.'/subtask')}}" class="editInline"><i class="glyphicon glyphicon-plus"></i></a>
            <a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/edit" class="editInline"><i class="glyphicon glyphicon-pencil"></i></a>
            <a href="" class="editInline"><i class="glyphicon glyphicon-trash"></i></a>
        </div>
    <h4><i class="fa fa-check-square-o"></i> <span><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a>

    </span></h4>
 </div>
 </li>
 </ul>
  <hr>
@endforeach

我需要这样一个刀片文件

@if (Auth::user()->id)->exists()) {
 @foreach ($project->tasks as $task)
  <ul>
    <li>
  <div>
  <div class="pull-right icons-align">
            <a href="{{url('projects/'.$project->id.'/task/'.$task->id.'/subtask')}}" class="editInline"><i class="glyphicon glyphicon-plus"></i></a>
            </div>
    <h4><i class="fa fa-check-square-o"></i> <span><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a>

    </span></h4>
 </div>
 </li>
 </ul>
  <hr>
@endforeach

else
 @foreach ($project->tasks as $task)
  <ul>
    <li>
  <div>
  <div class="pull-right icons-align">
            <a href="{{url('projects/'.$project->id.'/task/'.$task->id.'/subtask')}}" class="editInline"><i class="glyphicon glyphicon-plus"></i></a>
            <a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/edit" class="editInline"><i class="glyphicon glyphicon-pencil"></i></a>
            <a href="" class="editInline"><i class="glyphicon glyphicon-trash"></i></a>
        </div>
    <h4><i class="fa fa-check-square-o"></i> <span><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a>

    </span></h4>
 </div>
 </li>
 </ul>
  <hr>
@endforeach

我该怎么办?

bejyjqdl

bejyjqdl1#

下面展示了如何使用if语句laravel blade
可以使用@if、@elseif、@else和@endif指令构造if语句。这些指令的功能与它们的PHP对应指令相同:

@if (count($records) === 1)
    <p>I have one record!</p>
@elseif (count($records) > 1)
    <p>I have multiple records!</p>
@else
   <p> I don't have any records! </p>
@endif

更多详情:https://laravel.com/docs/5.5/blade

j1dl9f46

j1dl9f462#

我在你的代码中发现了一些错误,比如else没有@,你在first if中使用了{,这是不应该使用的。你也错过了关闭@endif
这里是代码

@if (Auth::user()->id)->exists()) 
 @foreach ($project->tasks as $task)
  <ul>
    <li>
  <div>
  <div class="pull-right icons-align">
            <a href="{{url('projects/'.$project->id.'/task/'.$task->id.'/subtask')}}" class="editInline"><i class="glyphicon glyphicon-plus"></i></a>
            </div>
    <h4><i class="fa fa-check-square-o"></i> <span><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a>

    </span></h4>
 </div>
 </li>
 </ul>
  <hr>
@endforeach

@else
 @foreach ($project->tasks as $task)
  <ul>
    <li>
  <div>
  <div class="pull-right icons-align">
            <a href="{{url('projects/'.$project->id.'/task/'.$task->id.'/subtask')}}" class="editInline"><i class="glyphicon glyphicon-plus"></i></a>
            <a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/edit" class="editInline"><i class="glyphicon glyphicon-pencil"></i></a>
            <a href="" class="editInline"><i class="glyphicon glyphicon-trash"></i></a>
        </div>
    <h4><i class="fa fa-check-square-o"></i> <span><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a>

    </span></h4>
 </div>
 </li>
 </ul>

  <hr>
@endforeach
@endif

相关问题