我如何通过表值应用条件?

时间:2017-10-20 15:44:47

标签: php laravel laravel-5 carousel laravel-5.4

我正在制作一个旋转木马,它应该在其中动态显示信息。在这里,我试图应用条件,通过该条件可以过滤结果,但它似乎并没有起作用。下面是它的代码。(通过这个我真正想要实现的是在旋转木马中显示结果)。任何形式的帮助都将受到极大的赞赏

     <div class="container">
        <div class="row" style="padding: 75px">
            @if($tours->continent_id == 5)

            <div class="row">
                <div class="col-md-9">
                    <h3>Featured {{$tours->continent->name}}</h3>                
            </div>
                <div class="col-md-3">
                    <!-- Controls -->
          <div class="controls pull-right hidden-xs">
                        <a class="left fa fa-chevron-left btn btn-success" 
           href="#carousel-example"
                           data-slide="prev"></a><a class="right fa fa-
             chevron-
           right btn btn-success" href="#carousel-example"
                                                    data-slide="next"></a>
                    </div>
                </div>
            </div>
          <div id="carousel-example" class="carousel slide hidden-xs" data-
          ride="carousel">
                <!-- Wrapper for slides -->
                <div class="carousel-inner">
                    <div class="item active">
                        <div class="row">
                            @foreach($tours as $tour)
                            <div class="col-sm-3">
                                <div class="col-item">
                                    <div class="photo">
                                        <img src="{{$tour->photo->file ? 
                  asset($tour->photo->file): asset('images/404.png')}}" 
            class="img-responsive" alt="a" />
                                    </div>
                                    <div class="info">
                                        <div class="row">
                                            <div class="price col-md-6">
                                                <h5>{{$tour->location}}</h5>
                                                <h5 class="price-text-
                   color">
                                                    {{$tour->service}}</h5>
                                            </div>
                                            <div class="rating hidden-sm 
                 col-md-
                        6">
                                                <i class="price-text-color 
           fa 
          fa-star"></i><i class="price-text-color fa fa-star">
                                            </i><i class="price-text-color 
         fa 
         fa-star"></i><i class="price-text-color fa fa-star">
                                            </i><i class="fa fa-star"></i>
                                            </div>
                                        </div>
                                        <div class="separator clear-left">
      <p class="btn-add"> <i class="fa fa-shopping-cart"></i><a 
     href="http://www.jquery2dotnet.com" class="hidden-sm">Add to cart</a>
    </p>

    <p class="btn-details"> <i class="fa fa-list"></i><a 
      href="http://www.jquery2dotnet.com" class="hidden-sm">More details</a>
      </p>
                                        </div>
                                        <div class="clearfix">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            @endforeach
                        </div>
                    </div>
       @endif
        </div>    
        </div>

Here is the controller part of the code. which is including all the fillable values in the form view. which later can be used to apply conditions on the form or return it's table values in the form 


    public function index()
    {$tours = Tour::all();
        return view('admin/tour/slider',compact('tours'));}

错误页面error message

2 个答案:

答案 0 :(得分:0)

您想要的是过滤游览,只与包含continent_id等于5的游戏进行互动?

在这种情况下,您应该改进对数据库的查询

$tours = Tour::where('continent_id', 5)->get();

或过滤集合

$tours->filter(function ($tour) {

    return $tour->continent_id == 5;
}

在这两种情况下,由于您使用{{ $tours->continent->name }} Collection,而不是使用Tour实例,因此无法执行Tour。< / p>

为了打印你应该做的第一次巡演的标题

$tours->first()->continent->name

答案 1 :(得分:0)

我所要做的就是将foreach语句之后的条件放在foreach循环之前,它的值可用于循环,我必须检查表中是否存在值@if($ tours) )。这一切都很顺利:))