方法追加不存在Laravel

时间:2017-03-08 13:51:40

标签: laravel laravel-5 laravel-4 laravel-5.2

我想选择0到49之间的所有酒店,它完全是marche,但是当我想选择所有在0-49到50-99之间的最小值的酒店时,你可以说当它进入循环时#34 ;对于"它会返回此错误

  

Macroable.php第74行中的ErrorException:方法追加不存在。 (查看:C:\ xampp \ htdocs \ elasticsearch \ resources \ views \ presult.blade.php)(查看:C:\ xampp \ htdocs \ elasticsearch \ resources \ views \ presult.blade.php)

功能Laravel

public function pricefilter(Request $request)
{
    $query = Input::get('query', false);
    $brans = request()->priceFilter;
    $books = new Collection();

    if (!empty($brans)) { 
        $product = "";

        foreach ($brans as $data) {
            $minMax    = explode("-",$data);
            $product[] = Afica::search()
                             ->multiMatch(['name','city_hotel'], $query, ['fuzziness' => 'AUTO'])
                             ->filter()
                             ->range('minrate',['from'=>$minMax[0],'to'=>$minMax[1]])
                             ->paginate(26)
                             ->appends('priceFilter' , request('priceFilter'));
        }

        $books = $product[0];

        for ($i = 1; $i < count($product); $i++) {
              $books = $books->union($product[$i]);
        }
   } else {
        $books = Afica::search()
                 ->paginate(26);
   }

   if ($request->ajax()) {
       return view('presult', compact('books'));
   }

   return view('welcome',compact('books')); 
}

查看主张

 <!--deal-->    
            <?php $myArray = array();?>

            @if  (empty($myArray))

            @foreach($books as $Afica)
            <?php $collection = collect($Afica);?>
            <article class="one-third">
              <figure><a href="#" title=""><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></a></figure>

              <div class="details">

                <h3>{{{  $collection->get('name') }}}
                  <span class="stars">

                      <?php 

                      for ($i=1 ; $i<= $collection->get('class') ; $i++) 
                      {
                      echo ' <i class="material-icons">&#xE838;</i>';
                      }

                  ?>
                  </span>
                </h3>
                <span class="address">{{{  $collection->get('city_hotel') }}},{{{  $collection->get('address') }}}  <a href="">Show on map</a></span>
                <span class="rating">{{{  $collection->get('preferred') }}}</span>
                <span class="price">Max rate  <em>$ {{{  $collection->get('maxrate') }}}</em> </span>
                <span class="pricee">Min rate  <em>$ {{{  $collection->get('minrate') }}}</em> </span>
                <div class="description">
                  <p>{{{  $collection->get('desc_en') }}}<a href="hotel?query= $collection->get('name')">More info</a></p>
                </div>   
                <a href="{{{ $collection->get('hotel_url') }}}" title="Book now" class="gradient-button">Book now</a>
              </div>
            </article><?php $myArray[] =  $collection->get('id');?>

            <!--//deal-->
            @endforeach

           @endif




            <!--//bottom navigation-->
             <div class="bottom-nav">
            <a href="#" class="scroll-to-top" title="Back up">Back up</a> 
            <div class="pager">
            <?php $query=Input::get('query', '');?>
            @if($query == "")
          <span><a href="?page=1">First Page</a></span>
            @else
         <span><a href="?query=<?php echo $query?>&page=1"=>FirstPage</a></span>
         @endif
       {!! $books->appends(['query' => Input::get('query')])->render() !!}
       @if($query == "")
          <span><a href="?page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
            @else
         <span><a href="?query=<?php echo $query?>&page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
         @endif
            <!--bottom navigation-->
              </div>
         </div>    
            <!--//bottom navigation-->

1 个答案:

答案 0 :(得分:1)

您似乎在视图中的多个位置调用名为->appends的变量上的$books

如果$books为空(即null),则可能会导致您的问题。

如果将设置函数作为起点,我建议将$books包裹起来

if (isset($books) && $books != NULL) {
    $books->appends(['query' => Input::get('query')])->render()
} else {
    // Do something which tells the user that there are no books
}

更好的解决方案是在控制器中处理视图的所有逻辑,并使用@forelse循环而不是@foreach循环。这样可以更轻松地处理返回空集合的情况。

@forelse ($books as $book)
    // Do your for each book markup
@empty
    // Do markup which shows when you have no books
@endforelse

对模板的更正

<?php $myArray = array(); ?>

@if  (empty($myArray))
    @foreach($books as $Afica)
    <?php $collection = collect($Afica);?>
    <article class="one-third">
        <figure><a href="#" title=""><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></a></figure>
        <div class="details">
            <h3>{{{  $collection->get('name') }}}
                <span class="stars">
                    <?php 

                    for ($i=1 ; $i<= $collection->get('class') ; $i++) {
                          echo ' <i class="material-icons">&#xE838;</i>';
                    }

                    ?>
                </span>
            </h3>
        </div>
        <span class="address">{{{  $collection->get('city_hotel') }}},{{{  $collection->get('address') }}}  <a href="">Show on map</a></span>
        <span class="rating">{{{  $collection->get('preferred') }}}</span>
        <span class="price">Max rate  <em>$ {{{  $collection->get('maxrate') }}}</em> </span>
        <span class="pricee">Min rate  <em>$ {{{  $collection->get('minrate') }}}</em> </span>

        <div class="description">
            <p>{{{  $collection->get('desc_en') }}}<a href="hotel?query= $collection->get('name')">More info</a></p>
        </div> 
        <div>  
            <a href="{{{ $collection->get('hotel_url') }}}" title="Book now" class="gradient-button">Book now</a>
        </div>
    </article><?php $myArray[] =  $collection->get('id');?>

    @endforeach
@endif

<div class="bottom-nav">
    <a href="#" class="scroll-to-top" title="Back up">Back up</a> 
    <div class="pager">
        <?php $query=Input::get('query', '');?>
        @if($query == "")
            <span><a href="?page=1">First Page</a></span>
        @else
            <span><a href="?query=<?php echo $query; ?>&page=1">FirstPage</a></span>
        @endif

        {!! $books->appends(['query' => Input::get('query')])->render() !!}

        @if($query == "")
            <span><a href="?page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
        @else
            <span><a href="?query=<?php echo $query; ?>&page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
        @endif
    </div>
</div>