Home > Blockchain >  I am using laravel 5.8 while creating the dynamic dropdown for search filter I get an error for Unde
I am using laravel 5.8 while creating the dynamic dropdown for search filter I get an error for Unde

Time:04-27

I already define the variable here is my code

class MainController extends Controller
{
    public function index()
    {
        $country = Country::all();
        return view ('index',compact($country)); 
    }

    public function getStates($id)
    {
       $states = State::where('country_id', $id)->pluck("name", "id");
       return json_encode($states);
    }
}

CodePudding user response:

you can do any of the following

1.

public function index()
    {
        $country = Country::all();
        return view('index',compact('country')); 
    }
public function index()
{
    $country = Country::all();
    return view('index',['country' => $country]); 
}

CodePudding user response:

when use compact write data withoute $ and put it in '' :

 public function index()
{
    $country = Country::all();
    return view ('index',compact('country')); 
}
  • Related