doue8385
doue8385
2019-03-28 04:22
浏览 36
已采纳

如果声明没有显示其他结果

The else section of my code is not being displayed in my laravel blade. I need to get the else section of the statement to show. currently, that button is missing when the if statement is false.

@auth
    @if(optional($quote->userLike))
        @foreach($quote->userLike as $like)
            @if($like->pivot->user_id == $id) //passing user  id from controller here
            <li class="list-inline-item">
                <form  method="POST" action="{{route('likequote')}}" class="likequote">
                    @csrf
                    <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                    <button type="submit" class="btn btn-danger shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
                </form>
            </li>
            @endif
        @endforeach 
    @else
    <li class="list-inline-item">
        <form  method="POST" action="{{route('likequote')}}" class="likequote">
            @csrf
            <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
            <button type="submit" class="btn btn-primary shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
        </form>
    </li>
    @endif  
@endauth

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucai5315
    doucai5315 2019-03-28 04:27
    已采纳
    • first method
    @auth
        @forelse($quote->userLike as $like)
                @if($like->pivot->user_id == $id) //passing user  id from controller here
                <li class="list-inline-item">
                    <form  method="POST" action="{{route('likequote')}}" class="likequote">
                        @csrf
                        <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                        <button type="submit" class="btn btn-danger shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
                    </form>
                </li>
                @endif
        @empty
        <li class="list-inline-item">
            <form  method="POST" action="{{route('likequote')}}" class="likequote">
                @csrf
                <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                <button type="submit" class="btn btn-primary shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
            </form>
        </li>
        @endforelse
    @endauth
    
    • Second
    @auth
        @if(!empty($quote->userLike) && count($quote->userLike)>0)
            @foreach($quote->userLike as $like)
                @if($like->pivot->user_id == $id) //passing user  id from controller here
                <li class="list-inline-item">
                    <form  method="POST" action="{{route('likequote')}}" class="likequote">
                        @csrf
                        <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                        <button type="submit" class="btn btn-danger shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
                    </form>
                </li>
                @endif
            @endforeach 
        @else
        <li class="list-inline-item">
            <form  method="POST" action="{{route('likequote')}}" class="likequote">
                @csrf
                <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                <button type="submit" class="btn btn-primary shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
            </form>
        </li>
        @endif  
    @endauth
    
    点赞 评论
  • doushi9444
    doushi9444 2019-03-28 12:27
        You can try this   
    
     @auth
         @isset($quote)
            @if($quote->userLike->count() > 0)
                @foreach($quote->userLike as $like)
                    @if($like->pivot->user_id == $id) //passing user  id from controller here
                    <li class="list-inline-item">
                        <form  method="POST" action="{{route('likequote')}}" class="likequote">
                            @csrf
                            <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                            <button type="submit" class="btn btn-danger shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
                        </form>
                    </li>
                    @endif
                @endforeach 
            @else
            <li class="list-inline-item">
                <form  method="POST" action="{{route('likequote')}}" class="likequote">
                    @csrf
                    <input type="hidden" class="form-control col-md-4" name="likequote" id="likequote" value='{{$quote->id}}'>
                    <button type="submit" class="btn btn-primary shadow-lg " id="savelike"><i class="fas fa-heart"></button></i>
                </form>
            </li>
            @endif  
         @endisset
        @endauth
    
    点赞 评论

相关推荐