How to add Division district upozil one by under one



Migration Schema


Schema::create('divisions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('division_name')->nullable();
            $table->timestamps();
        });

Schema::create('districts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('district_name')->nullable();
            $table->integer('divisions_id')->unsigned();
            $table->timestamps();
        
         $table->foreign('divisions_id')->references('id')->on('divisions');
        
        });

Schema::create('upozilas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('upozila_name')->nullable();
            $table->integer('divisions_id')->unsigned();
            $table->integer('districts_id')->unsigned();
            $table->timestamps();

            $table->foreign('divisions_id')->references('id')->on('divisions');
            $table->foreign('districts_id')->references('id')->on('districts');
        });
 

Model

divisions.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Divisions extends Model
{
    protected $fillable=['division_name'];
}


districts.php


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Districts extends Model
{
    protected $fillable=['district_name','divisions_id'];
}

upozila.php


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Upozilas extends Model
{
   protected $fillable=['upozila_name','divisions_id','districts_id'];
}
 

Division Controller


<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Divisions;
class DivisionController extends Controller
{
    
    public function index()
    {
        //
    }

   
    public function create()
    {
        return view('division.create');
    }

    
    public function store(Request $request)
    {
        $this->validate($request,[
               'division_name'=>'required|unique:divisions',
         ]);
         $data=$request->all();
         Divisions::create($data);
         return redirect()->back()->with('success','Division Add Successfully.');
    }
}

Disrict Controller


<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Districts;
use App\Divisions;
class DistrictController extends Controller
{
   
    public function index()
    {
        //
    }

    
    public function create()

    {
        $divisions=Divisions::all();
        return view('district.create',compact('divisions'));
    }

   
    public function store(Request $request)
    {
        $this->validate($request,[
               'district_name'=>'required|unique:districts',
               'divisions_id'=>'required',
         ]);
         $data=$request->all();
         Districts::create($data);
           return redirect()->back()->with('success',' Add Successfully.');
    }

}

















Upozila Controller


<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Upozilas;
use App\Divisions;
class UpozilaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
          $divisions=Divisions::all();
        return view('upozila.create',compact('divisions'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'upozila_name'=>'required',
            'divisions_id'=>'required',
            'districts_id'=>'required',

        ]);
        $data=$request->all();
        Upozilas::create($data);

        return redirect()->back()->with('success',' Add Successfully.');
    }

}

Address Controller


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
  use Illuminate\Support\Facades\Input;
  use App\Divisions;
  use App\Districts;
  use App\Upozilas;

class AddressController extends Controller
{
    public function view()
    {
         $divisions=Divisions::all();
        return view('address',compact('divisions'));
    }
   
    public function division()
    {
       
    }
    public function district()
    {
        $divisions_id = Input::get('divisions_id');
      $districts = Districts::where('divisions_id', '=', $divisions_id)->get();
      return response()->json($districts);
    }

    public function upozila()
    {
        $districts_id = Input::get('districts_id');
      $upozilas = Upozilas::where('districts_id', '=', $districts_id)->get();
      return response()->json($upozilas);
    }
}

###Note :  Laravel 7 change Input syntax    

   public function upozila(Request $request)

$districts_id = Input::get('districts_id'); change to    $districts_id =$request->input('districts_id ');

Route


Route::resource('divisions','Backend\DivisionController');
Route::resource('districts','Backend\DistrictController');
Route::resource('upozilas','Backend\UpozilaController');

Route::get('/json-districts','AddressController@district');
Route::get('/json-upozila','AddressController@upozila');

Route::get('/address','AddressController@view');

address.lade.php

 <!DOCTYPE html>
<html lang="en">
<head>
  <title>Division</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
@if(Session::has('success'))
    <div class="alert alert-success">
        {{ Session::get('success') }}
    </div>

@endif
@if(Session::has('warning'))
    <div class="alert alert-danger">
        {{ Session::get('warning') }}
    </div>

@endif
<div class="container">
  <h2>Upozila add</h2>
  <form action="/upozilas" method="POST">

      {{csrf_field()}}
<div class="form-group">
     <select class="form-control" name="divisions_id" id="division">
        <option value="">
          Please select a division
        </option>


           @foreach($divisions as $div)
     <option value="{{$div->id}}">{{$div->division_name}}</option>
           @endforeach

      </select>
</div>

 <div class="form-group">
  <select name="districts_id" id="district" class="form-control">
   
    <option value="">Please select a district</option>
  </select>
  </div>

  <div class="form-group">
  <select name="districts_id" id="upozila" class="form-control">
   
    <option value="">Please select a Upozila</option>
  </select>
  </div>

    <div class="form-group">
      <label for="email"> Name</label>
      <input type="text" class="form-control" id="" placeholder="upozila name" name="upozila_name">
    </div>
   
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
            <script type="text/javascript">
               
                $('#division').on('change',function(e){
                    console.log(e);

                    var divisions_id= e.target.value;

                    $.get('/json-districts?divisions_id=' + divisions_id,function(data){
                        console.log(data);

                     $('#district').empty();
          $('#district').append('<option value="0" disable="true" selected="true">=== Select Districts ===</option>');

          $.each(data, function(index, districtsObj){
            $('#district').append('<option value="'+ districtsObj.id +'">'+ districtsObj.district_name +'</option>');
          });

                    });
                });

                 $('#district').on('change',function(e){
                    console.log(e);

                    var districts_id= e.target.value;

                    $.get('/json-upozila?districts_id=' + districts_id,function(data){
                        console.log(data);

                     $('#upozila').empty();
          $('#upozila').append('<option value="0" disable="true" selected="true">=== Select Upozila ===</option>');

          $.each(data, function(index, districtsObj){
            $('#upozila').append('<option value="'+ districtsObj.id +'">'+ districtsObj.upozila_name +'</option>');
          });

                    });
                });
            </script>

</body>
</html>

বাসা ভাড়ার সহজ মাধ্যম বাসা বাড়ি বিডি ডট কম  https://basabaribd.com





 দৈনিক সংবাদ   https://jiboneralo.com

বিভিন্ন চাকরির পরীক্ষার জন্য প্রস্তুতি নিতে https://schoolportalbd.com


No comments

Powered by Blogger.