user image

Omosanya Olamilekan
Published in : 2022-03-16

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

web-brackets.com

Comments

Mohamed Atef Date : 2022-03-16

Best answers

46

Best answers

46

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

Omosanya Olamilekan Date : 2022-03-17

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 Date : 2022-03-17

Sorry I mean web.php 

Omosanya Olamilekan Date : 2022-03-17

Best answers

1

Best answers

1

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 >
      <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] Nginx 413 Request Entity Too Large - Laravel
Publish date: 2022-02-01 | Comments: 2
How to get original attribute value in Laravel?
Publish date: 2022-02-23 | Comments: 3
laravel installation error with composer install
Publish date: 2022-02-23 | Comments: 2
[solved] SQLSTATE[42000]: Syntax error or access violation
Publish date: 2022-01-20 | Comments: 2
[SOLVED] No application encryption key has been specified.
Publish date: 2021-05-17 | Comments: 2
How can i pass data from blade template to vue component?
Publish date: 2022-02-11 | Comments: 1
tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0
Publish date: 2022-02-18 | Comments: 2