Home > front end >  Undefined variable $result (View:/resources/views/frontend/CustomerRequest.blade.php)
Undefined variable $result (View:/resources/views/frontend/CustomerRequest.blade.php)

Time:10-04

when i run code then it gives this error: "Undefined variable $result (View:/resources/views/frontend/CustomerRequest.blade.php) ", anyone can plz suggest any solution. Thanks

controller:

   public function index()
   {
      $result =  DB:: select('select * from form_request');
      $result= form::all();
      return view('frontend.CustomerRequest')->with('result', $result);
   }

view:

  @foreach($result as $form)
       
      <tr>
          <td>{{ $form->id }}</td>
          <td>{{ $form->name }}</td>
      </tr>
   @endforeach

route:

  Route::get('customerrequest',[PostsController::class, 'index']);

CodePudding user response:

Using the syntax return view('frontend.CustomerRequest')->with('result', $result); is wrong, that way does not pass the variable to the view page.

Instead it should be return view('frontend.CustomerRequest', compact('result');

So change your controller function to this

   public function index()
   {
      $result =  DB:: select('select * from form_request');
      $result= form::all();
      return view('frontend.CustomerRequest', compact('result');
   }

Now in your view file you should be able to access the $result variable.

  • Related