0

In my Blade file, there are two dropdowns, "Countries" and "Regions". I want the Regions dropdown only to display the values linked to the country selected in the "Countries" dropdown.

For that, I believe, I have to pass a variable for $countries and $regions in order to compact them and call the variables in the blade file.

This is what I currently have (which is totally wrong), but what should it be?

    public function create()
    {
        $countries = Countries::orderBy('name')->get();
        $regions = Regions::where('countries_id' == $countries('id'))->orderBy('name')->get();
        return view('admin.cities.create', compact('regions', 'countries'));
    }

I know that the $regions variable part "where('countries_id' == $countries('id'))" is incorrect, but I've tried multiple other ways, and still not able to figure it out.

Is this variable = model::where(foreign_key == parent_table ID) possible?

All my relationships have been set up with Laravel Eloquent already.

Any assistance would be appreciated

1 Answers1

1

I started to write how to fetch data from controller(Country::all and Regions::all), and what to do in blade file and ajax functions that does stuff on events and figured - maybe there already is a solution.

Sorry, I don't have 50 rep to comment, but if you follow what they did here (both answers), I think you're gonna figure out what to do in your example :)

Keep in mind you need the part in the bottom of the page!

Laravel dynamic dropdown country and state