Laravel Select Dropdown From Database ExampleQuestion: how to retrieve data from database whenever select in drop down list in laravel? Show
Read Also: Select DropDown Option Selected In Laravel Laravel Controller Code Example 1 : Using Form Example 2 : Without Using Form I hope you get an idea about create dynamic select box with Form class. Read Also: Laravel Select Dropdown From Database Example Related posts:
Read Also: Group by and Having Clause in SQL Next Read: How To Pass Data To Views from controller In Laravel? » “how to retrieve data from database whenever select in drop down list in laravel” Code Answerhow to retrieve data from database using select option in laravel php by Detetive on Sep 22 2021 Comment 0 Source: www.pakainfo.com Add a Grepper Answer PHP answers related to “how to retrieve data from database whenever select in drop down list in laravel”
PHP queries related to “how to retrieve data from database whenever select in drop down list in laravel”
Laravel 8 Dynamic Dependent Dropdown using Ajax Tutorial
Step 1 – Install Laravel 8 AppFirst of all, Execute the following command on terminal to download or install laravel 8 fresh new setup: composer create-project --prefer-dist laravel/laravel blogStep 2 – Connecting App to DatabaseAfter that, open “.env” file and update the database name, username, and password in the env file: DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_PasswordRecommended:- Laravel 8 Ajax Image Upload with Preview Tutorial Step 3 – Create Modal and MigrationIn this step, create category table migration and create category Modal by using the following command: php artisan make:model Category -mNavigate database/migrations/ and open create_categorys_table.php file. Then update the following code into this file: public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name'); $table->unsignedInteger('parent_id')->nullable(); $table->timestamps(); }); } Now run the following command php artisan migrateRecommended:- Laravel 8 Livewire CRUD with Jetstream Tutorial Next, open Category.php model file and update the following code into it, which is placed on app/Models/: <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Category extends Model { use HasFactory; public function subcategories(){ return $this->hasMany('App\Category', 'parent_id'); } } Step 4 – Add RoutesNext step, Navigate to “routes/web.php” file and add the following routes into your web.php file: use App\Http\Controllers\CategoryController; Route::get('cat', [CategoryController::class, 'index']); Route::post('subcat', [CategoryController::class, 'subCat']);Step 5 – Create Controllers by ArtisanNext step, execute the following command on terminal to create controller file that named CategoryController: php artisan make:controller CategoryControllerThis command will create CategoryController by the artisan command. Next, Navigate to app/http/controller and open CategoryController.php.Then update the following methods into your controller file: <?php namespace App\Http\Controllers; use App\Models\Category; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; class CategoryController extends Controller { public function index(Request $request) { $categoris = Category::where('parent_id',0)->get(); return view('category',["categoris" => $categoris]); } public function subCat(Request $request) { $parent_id = $request->cat_id; $subcategories = Category::where('id',$parent_id) ->with('subcategories') ->get(); return response()->json([ 'subcategories' => $subcategories ]); } } Recommended:- Laravel 8 PHP Guzzle Http Client GET & POST Example Step 6 – Create Blade ViewsIn this step, create one blade views file for rendering data on it. So navigate to resources/views folder and create the blade view as following: Create first file name category.blade.php and update the following code into it: <html lang="{{ app()->getLocale() }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel 8 jquery ajax categories and subcategories, select dropdown</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"> <meta name="csrf-token" content="{{ csrf_token() }}" /> </head> <body> <div class="container" style="margin-top: 50px; margin-left: 300px"> <div class="row"> <div class="col-lg-6"> <form action=""> <h4>Category</h4> <select class="browser-default custom-select" name="category" id="category"> <option selected>Select category</option> @foreach ($categoris as $item) <option value="{{ $item->id }}">{{ $item->name }}</option> @endforeach </select> <h4>Subcategory</h4> <select class="browser-default custom-select" name="subcategory" id="subcategory"> </select> </form> </div> </div> </div> <script type="text/javascript"> $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $(document).ready(function () { $('#category').on('change',function(e) { var cat_id = e.target.value; $.ajax({ url:"{{ route('subcat') }}", type:"POST", data: { cat_id: cat_id }, success:function (data) { $('#subcategory').empty(); $.each(data.subcategories[0].subcategories,function(index,subcategory){ $('#subcategory').append('<option value="'+subcategory.id+'">'+subcategory.name+'</option>'); }) } }) }); }); </script> </body> </html> Recommended:- Laravel Restrict IP Address From Accessing Website Step 7 – Run Development ServerIn this step, use the following php artisan serve command to start your server locally: php artisan serveStep 8 – Test This AppNow, open browser and hit the following url on it for test this app: http://localhost:8000/cat Contents
1. Database ConfigurationOpen .env file. Specify the host, database name, username, and password. DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=2. Table structure
|