Omosanya Olamilekan

16 Mar 2022

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

Laravel

HERE IS MY CONTROLLER : HomeController

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

Comments

Mohamed Atef

16 Mar 2022

githubgithubgithub

Hello Omosanya, Welcome to Web-Brackets.com 

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

Replies

Omosanya Olamilekan

17 Mar 2022

this is my routeServiceProvider.php

<?php

namespace 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

17 Mar 2022

githubgithubgithub

Sorry I mean web.php 

Omosanya Olamilekan

17 Mar 2022

Best Answer

best answer

it is not showing the error again. 

<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
//include homecontroller in web.php
use 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 function
Route::get('auth.register','App\Http\Controllers\HomeController@getStates');
Route::get('/getLocalgovernments/{id}','App\Http\Controllers\HomeController@getLocalgovernments');
Route::get('/getWards/{id}','App\Http\Controllers\HomeController@getWards');
Route::get('/getPollingunits/{id}','App\Http\Controllers\HomeController@getPollingunits');

// setting route for home page
Route::get('/register' ,[HomeController::class, 'State']); // i didnt add this before. i just add ut and the error is not showing

Route::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 class="container">
 <h1>DropDown Feature</h1>
 <div class="form-group">
 <label for="state">Select your State</label>
 <select name="state" id="state" class="form-control">
 <option value="">Select State</option>
 @foreach( $states as $key => $value)
 <option value="{{$key}}">{{$value}}</option>
 @endforeach

 </select> 
 </div>
 <div class="form-group">
 <label for="LocalGovernment">Select your LG</label>
 <select name="LocalGovernment" id="LocalGovernment" class="form-control">
 <option value="">Select State</option> 
 </select> 
 </div>
 <div class="form-group">
 <label for="Ward">Select your Wards</label>
 <select name="Ward" id="Ward" class="form-control">
 <option value="">Select City</option> 
 </select> 
 </div>

 <div class="form-group">
 <label for="PollingUnits">Select your Poll-units</label>
 <select name="PollingUnits" id="PollingUnits" class="form-control">
 <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>

© 2024 Copyrights reserved for web-brackets.com