-1

I am getting undefined variable error in Laravel for the code section: (this is index.blade.php file)

 @foreach($faqs as $faq) 

  <tr>
  <td>{{$loop->iteration}}</td>
  <td>{{$faq->question}}</td>
  <td>{{$faq->descripton}}</td>
  </tr>

though I send the variable in this file via Controller:

public function index(Request $request)
{

    $faqs = Faq::all();
    return view('admin.faq.index')->with('faqs', $faqs);
}

The Faq Model is :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Faq extends Model
{
    //
}

the error message I am getting is enter image description here

How can I solve this? TIA.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
Nabid Anzum
  • 250
  • 2
  • 13
  • `return view('admin.faq.index')->with(['faqs'=> $faqs]);` try this `->with() ` accept array – Kamlesh Paul Aug 24 '20 at 10:27
  • `return view('admin.faq.index')->with('faqs', $faqs);` is incorrect, it should be an array with key/value as `return view('admin.faq.index')->with('[faqs' => $faqs]);` – Qirel Aug 24 '20 at 10:36

3 Answers3

1

You need to pass the data in an array(->with() method) or you can use compact method too.

return view('admin.faq.index', compact('faqs')); 

Or

return view('admin.faq.index')->with(array('faqs'=>$faqs));
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • still getting same error. this is the file https://github.com/yasirarafat28/Royaltyexoticcars/blob/master/resources/views/admin/faq/index.blade.php – Nabid Anzum Aug 24 '20 at 10:33
  • Did you even read the answer before saying you still get an error? Because that's how you solve the error. – Qirel Aug 24 '20 at 10:37
  • @NabidAnzum you need to change it in your controller file you need to pass your `faqs` data in array if you are using `with()`. `->with('[faqs' => $faqs]);` – Dilip Hirapara Aug 24 '20 at 10:42
0

try to use the compact method as below:

 public function index(Request $request)
{

    $faqs = Faq::all();
    return view('admin.faq.index',compact('faqs'));
}
Naveed Ali
  • 1,043
  • 7
  • 15
-1

Looks like $faqs is empty. You can check that with @isset(). https://laravel.com/docs/7.x/blade#if-statements

Flo Espen
  • 450
  • 4
  • 10