Omosanya Olamilekan
Published in : 2022-03-16

For each loop to populate a list in my database as a drop down menu



usertype=='1') { return view('admin.home'); } if (Auth::user()->usertype=='0') { return view('dashboard'); } else { return index()->back(); } } public function index() { return view('welcome'); } // functions for state.lg... public function getStates(){ $states = State::all()->pluck('state_name','id'); return view('auth/register', ['states' => $states]); } public function getLocalgovernments($id){ $localgovernment= Localgovernment::where('state_id',$id)->pluck('LocalGovernment_name','id'); return json_encode($localgovernment); } public function getWards($id){ $wards= Ward::where('LocalGovernment_id',$id)->pluck('ward_name','id'); return json_encode($wards); } public function getPollingunits($id){ $pollingunits= Pollingunit::where('LocalGovernment_id',$id)->pluck('PollingUnits_name','id'); return json_encode($pollingunits); } }

and my web.php page code is:

get('/dashboard', function () { return view('dashboard');})->name('dashboard');

this is my register.blade.php

 DropDown Feature Select your State Select State @foreach( $states as $key => $value) {{$value}} @endforeach Select your LG Select State Select your Wards Select City Select your Poll-units Select PollingUnits $(document).ready(function(){ $('select[name="state"]').on('change',function(){ var state_id= $(this).val(); if (state_id ) { $.ajax({ url: "{{url('/ getLocalgovernments/')}}/"+state_id, type: "GET", dataType: "json", success: function(data){ console.log(data); $('select[name="LocalGovernment"]').empty(); $.each(data,function(key,value){ $('select[name="LocalGovernment"]').append(''+value+''); }); } }); }else { $('select[name="LocalGovernment"]').empty(); } }); $('select[name="LocalGovernment"]').on('change',function(){ var LocalGovernment_id= $(this).val(); if (LocalGovernment_id) { $.ajax({ url: "{{url('/getWards/')}}/"+LocalGovernment_id, type: "GET", dataType: "json", success: function(data){ console.log(data); $('select[name="Ward"]').empty(); $.each(data,function(key,value){ $('select[name="Ward"]').append(''+value+''); }); } }); }else { $('select[name="Ward"]').empty(); } }); $('select[name="PollingUnits"]').on('change',function(){ var PollingUnits_id= $(this).val(); if (PollingUnits_id) { $.ajax({ url: "{{url('/getPollingunits/')}}/"+PollingUnits_id, type: "GET", dataType: "json", success: function(data){ console.log(data); $('select[name="Ward"]').empty(); $.each(data,function(key,value){ $('select[name="Ward"]').append(''+value+''); }); } }); }else { $('select[name="Ward"]').empty(); } }); }); 

please any help will be appreciated


Mohamed Atef Date : 2022-03-16

Best answers


Best answers


Hello Omosanya, Welcome to 

I have some doubts that if you tried auth.register instead of auth/register this might help

 public function getStates(){ $states = State::all()->pluck('state_name','id'); return view('auth.register', ['states' => $states]); }

and PLEASE share the routes.php file because it seems that you are not connecting the Route to the correct function

Omosanya Olamilekan Date : 2022-03-17

this is my routeServiceProvider.php

<?phpnamespace App\Providers;use Illuminate\Cache\RateLimiting\Limit;use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;use Illuminate\Http\Request;use Illuminate\Support\Facades\RateLimiter;use Illuminate\Support\Facades\Route;class RouteServiceProvider extends ServiceProvider{ /** * The path to the "home" route for your application. * * This is used by Laravel authentication to redirect users after login. * * @var string */ public const HOME = '/home'; /** * Define your route model bindings, pattern filters, etc. * * @return void */ public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->group(base_path('routes/api.php')); Route::middleware('web') ->group(base_path('routes/web.php')); }); } /** * Configure the rate limiters for the application. * * @return void */ protected function configureRateLimiting() { RateLimiter::for('api', function (Request $request) { return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); }); }}

Thanks Sir

Mohamed Atef Date : 2022-03-17

Sorry I mean web.php 

Omosanya Olamilekan Date : 2022-03-17

Best answers


Best answers


it is not showing the error again. 

<?phpuse Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\Route;//include homecontroller in web.phpuse App\Http\Controllers\HomeController;/*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*///route for my state,lg functionRoute::get('auth.register','App\Http\Controllers\[email protected]');Route::get('/getLocalgovernments/{id}','App\Http\Controllers\[email protected]');Route::get('/getWards/{id}','App\Http\Controllers\[email protected]');Route::get('/getPollingunits/{id}','App\Http\Controllers\[email protected]');// setting route for home pageRoute::get('/register' ,[HomeController::class, 'State']); // i didnt add this before. i just add ut and the error is not showingRoute::get('/' ,[HomeController::class, 'index']);Route::get('/home' ,[HomeController::class, 'redirect']);Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () { return view('dashboard');})->name('dashboard');

thanks a lot, Sir. But I am having an issue with my ajax. it's showing  500 (Internal Server Error). this is my code:

<body> <div > <h1>DropDown Feature</h1> <div > <label for="state">Select your State</label> <select name=state id="state" > <option value="">Select State</option> @foreach( $states as $key => $value) <option value="{{$key}}">{{$value}}</option> @endforeach </select> </div> <div > <label for="LocalGovernment">Select your LG</label> <select name=LocalGovernment id="LocalGovernment" > <option value="">Select State</option> </select> </div> <div > <label for="Ward">Select your Wards</label> <select name=Ward id="Ward" > <option value="">Select City</option> </select> </div> <div > <label for="PollingUnits">Select your Poll-units</label> <select name=PollingUnits id="PollingUnits" > <option value="">Select PollingUnits</option> </select> </div> </div> <script> $(document).ready(function(){ $('select[name="state"]').on('change',function(){ var state_id= $(this).val(); if (state_id ) { $.ajax({ url: "{{url('/getLocalgovernments/')}}/"+state_id, type: "GET", dataType: "json", success: function(data){ console.log(data); $('select[name="LocalGovernment"]').empty(); $.each(data,function(key,value){ $('select[name="LocalGovernment"]').append('<option value="'+key+'">'+value+'</option>'); }); } }); }else { $('select[name="LocalGovernment"]').empty(); } }); $('select[name="LocalGovernment"]').on('change',function(){ var LocalGovernment_id= $(this).val(); if (LocalGovernment_id) { $.ajax({ url: "{{url('/getWards/')}}/"+LocalGovernment_id, type: "GET", dataType: "json", success: function(data){ console.log(data); $('select[name="Ward"]').empty(); $.each(data,function(key,value){ $('select[name="Ward"]').append('<option value="'+key+'">'+value+'</option>'); }); } }); }else { $('select[name="Ward"]').empty(); } }); $('select[name="PollingUnits"]').on('change',function(){ var PollingUnits_id= $(this).val(); if (PollingUnits_id) { $.ajax({ url: "{{url('/getPollingunits/')}}/"+PollingUnits_id, type: "GET", dataType: "json", success: function(data){ console.log(data); $('select[name="Ward"]').empty(); $.each(data,function(key,value){ $('select[name="Ward"]').append('<option value="'+key+'">'+value+'</option>'); }); } }); }else { $('select[name="Ward"]').empty(); } }); }); </script> </body></html>

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

[solved] All assets in laravel storage returns 404
Publish date: 2021-12-14 | Comments: 2
Laravel error 419 page expired
Publish date: 2021-04-10 | Comments: 2