Fetch data from database in dropdownlist in laravel

Laravel Select Dropdown From Database Example

Question: how to retrieve data from database whenever select in drop down list in laravel?

Read Also: Select DropDown Option Selected In Laravel

Laravel Controller Code

/** * Show records * * @return \Illuminate\Http\Response */ public function dropDownShow(Request $request) { $products = Product::pluck('name', 'id'); $selectedID = 2; return view('products.edit', compact('id', 'products')); }

Example 1 : Using Form

<div class="form-group"> {!! Form::Label('product', 'Product:') !!} {!! Form::select('product_id', $products, $selectedID, ['class' => 'form-control']) !!} </div>

Example 2 : Without Using Form

<select class="form-control" name="product_id"> <option>Select Product</option> @foreach ($products as $key => $value) <option value="{{ $key }}" {{ ( $key == $selectedID) ? 'selected' : '' }}> {{ $value }} </option> @endforeach </select>

I hope you get an idea about create dynamic select box with Form class.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also: Laravel Select Dropdown From Database Example

  1. Laravel Select Dropdown From Database Example
  2. Select DropDown Option Selected In Laravel
  3. how to get the selected value of dropdown in php without submit?
  4. jQuery Get Selected Option From Dropdown Example
  5. How to get selected value of dropdown in JavaScript/jQuery on change?
  6. Vue JS Get Value From Selected Dropdown
  7. Show country state city dropdown select box using javascript
  8. Angular 9/8 Select Dropdown | Get Selected DropDown Value on Change Event
  9. How to Get the Text Value of Selected Option using jQuery?
  10. Remove selected option jquery

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 Answer


how 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


  • dd laravel query
  • laravel old value for select option
  • laravel select columns
  • how to get data from a table in laravel
  • show data from a table in laravel 8
  • option value selected in laravel blade
  • How to request and display data from db in larave
  • populate old value of dropdown laravel
  • show selected value in dropdown laravel
  • eloquent with select
  • searchable dropdown laravel blade
  • laravel command Retrieve a specific option
  • how to use old for select in blade laravel
  • option selected in laravel blade
  • laravel with select
  • Form::select laravel
  • laravel query select from table where id != to another table id
  • laravel db drop table
  • how to update dropdown value in laravel

  • laravel select option selected from database
  • laravel 8 select option from database
  • laravel select dropdown from database
  • select data in laravel eloquent
  • how to fetch data from database in select box in laravel
  • value select laravel
  • how to get select value in laravel
  • laravel select option from database
  • select value db in laravel
  • insert select option into database laravel
  • laravel select data from database with where clause
  • db table select in laravel 8
  • laravel retrieve data from database and put it in option
  • select where datae laravel
  • how to fetch a select record to a select form in html from a database in laravel
  • taken selected option according to the database laravel
  • select list set selected value from database laravel
  • select value get laravel db
  • laravel on select database value each show data
  • get select value laravel
  • load select value from db laravel
  • show data where select laravel
  • db ::select all data laravel
  • how to select data from database in laravel 8
  • select laravel get values database
  • how to select data in laravel
  • how to select data in database by laravel
  • select database in laravel eloquent
  • how to select table and where laravel
  • select in select at laravel 8
  • store select option into database laravel
  • select value laravel
  • laravel db select all from table in blade
  • how to get data from a table through select query laravel
  • select data using where laravel
  • select data by db table in laravel
  • select data with laravel
  • select data from database in laravel
  • how to retrieve data from database whenever select in drop down list in laravel
  • laravel select option value from database
  • how to select data in database by laravel
  • select data from database laravel
  • select box value get database in laravel
  • selecting data from database laravel
  • how to display data from database in select option in laravel
  • select data from table in laravel
  • laravel 8 get select record
  • old data select in laravel
  • laravel selected option from database
  • show data from drop down list in laravel with db query
  • laravel select dropdown from database example
  • retrieve value from database in laravel dropdown list
  • get values from db::select in laravel
  • how to get select option in laravel
  • how to retrieve data from database whenever select in drop down list in laravel
  • get selected data from database to select list laravel
  • inssert select option in laravel into db
  • get select option values from databse in laravel
  • laravel create data select option
  • laravel select from database
  • laravel get data from selected option
  • select data from a database in laravel 8
  • select data from select in laravel
  • how to select all data in laravel
  • hop to select data in database by laravel
  • database data in select option using laravel
  • how to make select data from database in laravel
  • how to selected select data in laravel
  • get html in select laravel
  • how to write query for selected data in laravel
  • select all data from table in laravel
  • db::select("select * from users"); laravel 8 how to print data
  • select data as laravel
  • getting value of select in laravel 8
  • select data from table in laravel in controller
  • how to get data from select option in laravel
  • laravel selected option from database
  • select option in laravel from db
  • select option in laravel into db
  • get record column database select option laravel
  • select data from table laravel
  • select data laravel
  • laravel select from database
  • select item from database php laravel
  • laravel select data from sql
  • how to select data in laravel from table
  • getting select option in laravel
  • select in get works laravel
  • db::table to select data from database querying inside blade laravel
  • how to retrieve data from database whenever select in drop down list in laravel usin ajex
  • select data from select list and get data from database laravel depend on select list
  • laravel select data when using with()
  • how to insert select option value in database using php laravel
  • laravel select frome database
  • select dropdown value from database in laravel
  • laravel use database value for select field
  • select data elequent laravel
  • database value in option laravel
  • retrieve data from database to select laravel
  • get data to show in select from database in laravel 8
  • bring select from database laravel
  • laravel 8 set select option from database
  • select from database in laravel
  • select data using db laravel blade
  • how to select all data from query laravel controller
  • db::select laravel show data
  • select data from db laravel
  • get database value in select menu laravel
  • laravel select box from database
  • inertia select from database laravel
  • select record in laravel
  • select where db row laravel

Laravel 8 Dynamic Dependent Dropdown using Ajax Tutorial

  • Step 1 – Install Laravel 8 App
  • Step 2 – Connecting App to Database
  • Step 3 – Create Model and Migration
  • Step 4 – Add Routes
  • Step 5 – Create Controllers By Artisan
  • Step 6 – Create Blade Views
  • Step 7 – Run Development Server
  • Step 8 – Test This App

Step 1 – Install Laravel 8 App

First of all, Execute the following command on terminal to download or install laravel 8 fresh new setup:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Connecting App to Database

After 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_Password

Recommended:- Laravel 8 Ajax Image Upload with Preview Tutorial

Step 3 – Create Modal and Migration

In this step, create category table migration and create category Modal by using the following command:

php artisan make:model Category -m

Navigate 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 migrate

Recommended:- 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 Routes

Next 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 Artisan

Next step, execute the following command on terminal to create controller file that named CategoryController:

php artisan make:controller CategoryController

This 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 Views

In 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 Server

In this step, use the following php artisan serve command to start your server locally:

php artisan serve

Step 8 – Test This App

Now, open browser and hit the following url on it for test this app:

http://localhost:8000/cat

Contents

  1. Database Configuration
  2. Table structure
  3. Model
  4. Controller
  5. Route
  6. View
  7. Demo
  8. Conclusion

1. Database Configuration

Open .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

  • Createdepartmentsandemployeestable using migration and add some records.
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
  • Now, navigate todatabase/migrations/folder from the project root.
  • Find a PHP file that ends withcreate_departments_tableand open it.
  • Define the table structure in theup()method.
public function up() { Schema::create('departments', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); }
  • Similarly, find a PHP file that ends withcreate_employees_tableand open it.
  • Define the table structure in theup()method.
public function up() { Schema::create('employees', function (Blueprint $table) { $table->id(); $table->string('username'); $table->string('name'); $table->string('email'); $table->integer('department'); $table->timestamps(); }); }
  • Run the migration –
php artisan migrate
  • The table is been created and I added some records in it.