Home > Software design >  Undefined Variable error when using @foreach
Undefined Variable error when using @foreach

Time:05-15

I'm currently working on a project in laravel8 and i've been stuck here for so long and i can't see what the problem is ! i'm trying to make the tours page dynamic but i get this: ErrorException Undefined variable: tours (View: C:\xampp\htdocs\project\resources\views\livewire\explore-component.blade.php) HERE IS THE: 'explore-component.blade.php' file :

 @foreach ($tours as $tour)
                
                
                    <li >
                        <div >
                            <div >
                                <a href="#" title="{{ $tour->name }}">
                                    <figure><img src="{{ asset('assetss/images/products') }}/{{ $tour->image }}" alt="{{ $tour->name }}"></figure>
                                </a>
                            </div>
                            <div >
                                <a href="#" ><span>{{ $tour->name }}</span></a>
                                <div ><span >${{  $tour->price_per_person }}</span></div>
                                <a href="#" >Book Now</a>
                            </div>
                        </div>
                    </li>
                    @endforeach

HERE IS THE: 'ExploreComponent.php' file :

<?php
namespace App\Http\Livewire;
use App\Models\Tour;
use Livewire\Component;
use Livewire\WithPagination;

class ExploreComponent extends Component
 {  
   use WithPagination;
   public function render()
  {
     $tours = Tour::paginate(12);
     return view('livewire.explore-component', ['tours'=> $tours])->layout("livewire.explore- 
     component");
     }
 }

CodePudding user response:

try this

in ExploreComponent.php

<?php
namespace App\Http\Livewire;
use App\Models\Tour;
use Livewire\Component;
use Livewire\WithPagination;

class ExploreComponent extends Component
 {  
   use WithPagination;
   public function render()
  {
     $tours = Tour::paginate(12);
     return view('livewire.explore-component', ['tours'=> $tours]);
     }
 }

in explore-component.blade.php

<div>
@forelse($tours as $tour)
<li >
                        <div >
                            <div >
                                <a href="#" title="{{ $tour->name }}">
                                    <figure><img src="{{ asset('assetss/images/products') }}/{{ $tour->image }}" alt="{{ $tour->name }}"></figure>
                                </a>
                            </div>
                            <div >
                                <a href="#" ><span>{{ $tour->name }}</span></a>
                                <div ><span >${{  $tour->price_per_person }}</span></div>
                                <a href="#" >Book Now</a>
                            </div>
                        </div>
                    </li>
@empty
@endforelse
</div>

then call Component

<livewire:explore-component />

CodePudding user response:

You need to check if it is empty

@empty(!$tours)
    @foreach ($tours as $tour)
       <li >
                <div >
                    <div >
                        <a href="#" title="{{ $tour->name }}">
                            <figure><img src="{{ asset('assetss/images/products') }}/{{ $tour->image }}" alt="{{ $tour->name }}"></figure>
                        </a>
                    </div>
                    <div >
                        <a href="#" ><span>{{ $tour->name }}</span></a>
                        <div ><span >${{  $tour->price_per_person }}</span></div>
                        <a href="#" >Book Now</a>
                    </div>
                </div>
            </li>
    @endforeach
                    
@else
@endempty
  • Related