0
votes

I want to select all hotels where minrate between 0 and 49 it marche perfectly , but when i want to select all hotels where minrate between 0-49 and 50-99 It is you to say when it enters the loop "for" it return this error

ErrorException in Macroable.php line 74: Method appends does not exist. (View: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php) (View: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php)

Function 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')); 
}

View presult

 <!--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

1 Answers

1
votes

It looks like you are calling ->appends on your variable called $books in several places within your view.

If $books is empty (i.e. null) this might be causing your problems.

I would suggest wrapping your $books in if is set functions as a starting point

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

An even better solution would be to handle all of the logic for the view in your controller and use something like a @forelse loop instead of a @foreach loop. This would make it easier to handle situations where you return an empty collection.

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

Corrections to the template

<?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>