Search code examples
laravelvariablesroutesframeworkscrm

how can I resolve this while working with for else loop and compact function in Laravel?


I'm using laravel version 7.30.1. it shows the error undefined variable $services.
code for home controller.

class HelloController extends Controller
{
  public function services()
   {
      $services = [
          'service 1',
          'service 2'
      ];

        return view('/services',compact('services'));
   }

}

code for blade file.

@section('content')

    <H1>welcome to laravel 6 from services</H1>
    <ul>
        @forelse($services as $service)

        <li>{{ $service }}</li>

      @empty

        <li>this list is empty</li>

        @endforelse
    </ul>
@endsection

code for route file

route::view('/services','services');

Solution

  • route::view('/services','services');

    here your only loading view not controller that's why your data is not pass in to view

    use get req. which will be in controller then form controller load view with data

    Route::get('services',[\App\Http\Controllers\HelloController::class, 'services']);