mercredi 26 septembre 2018

How to populate data using cascading in dropdown in laravel?

this is my user_type table

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

Data in user_type table

this is dp and rta table

Schema::create('dp', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dp_id');
        $table->integer('name');
        $table->timestamps();
    });


 Schema::create('rta', function (Blueprint $table) {
        $table->increments('id');
        $table->string('rta_id');
        $table->integer('name');
        $table->timestamps();
    });

This is add_user.blade.php

<div class="control-group">
          <label class="control-label">Type</label>
          <div class="controls">
            <select name="user_type" id="user_type">
                <option selected disabled>Select User Type</option>
                @foreach($user_type as $key => $value)
                 <option value=""></option>
                @endforeach
            </select>
          </div>
        </div>
         <div class="control-group">
          <label class="control-label">User Name</label>
         <div class="controls">
            <select name="user_id" id="user_id">
               <option selected disabled>Select User Name</option>
            </select>
          </div>
        </div>

Add user function in UserController

public function addUser(Request $request){
   if($request->isMethod('post')){
       $data = $request->all();

          $this->validate($request, [
            'full_name' => 'required',
            'user_email'=> 'required',
            'user_type'=> 'required',
            'user_password' => 'required'
          ]);
         /* [
            'menu_id.required' => 'Menu Name is required',
            'title.required' => 'Title is required',
            'description.required' => 'Description is required'
        ]);*/

       $user = new User();
       $user->type = $data['user_type'];
       $user->name = $data['full_name'];
       $user->user_id = $data['user_id'];
       $user->email = $data['user_email'];
       $password = Hash::make('user_password');
       $user->password = $password;
       $user->save();

       return redirect('/admin/view-user')->with('flash_message_success','User Added Successfully..');
   } 
     $user_type = DB::table('user_type')->pluck("title","id");
     return view('admin.user.add_user')->with(compact('user_type'));

}

Here, in this case, when i select dp in type drop down, i wan to show name from dp table in user_id drop down and when rta is selected, i want to show name from rta table in user_id drop down.

How to solve this??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire