dimanche 1 septembre 2019

Laravel - Dyanmic Dropdown-list with a relational database

I'm just starting out laravel and I'm confused on how do I start configuring my drop-down list. I need to create a position with a department and section drop-down.

Here is the first filter to be used. Department Model

use App\SectionModel;

class DepartmentModel extends Model {
    protected $table = 'tbl_department';
    protected $fillable = ['department_name'];

    public function section () {
        return $this->hasMany('App\SectionModel', 'department_id'); 
    }
}

Second selection would be based from the selected department. Section Model

use App\PositionModel;

class SectionModel extends Model {
    protected $table = 'tbl_section';
    protected $fillable = ['department_id', 'section_name'];

    public function department () {
        return $this->belongsTo('App\DepartmentModel');
    }

    public function position () {
        return $this->hasMany('App\PositionModel', 'section_id');
    }
}

Position Model

class PositionModel extends Model {
  protected $table = 'tbl_position';
  protected $fillable = ['section_id', 'position_name', 'n_level', 'job_description'];

  public function section () {
    return $this->belongsTo('App\SectionModel', 'id');
  }
}

Position Controller

use Illuminate\Http\Request;
use App\PositionModel;
use App\EmployeeModel;
use App\DepartmentModel;
use App\SectionModel;

class PositionController extends Controller
{

  public function index()
  {
    $getEmployee = EmployeeModel::all();
   return view('DashboardView.main', compact('getEmployee'));
  }

  public function create()
  {
    $getPosition = PositionModel::with('section')->get();
    $getDepartment = DepartmentModel::all();
    $getSection = SectionModel::all();
    return view('PositionView.add', compact('getPosition', 'getDepartment', 'getSection'));
  }

Add.blade.php

            <div class="form-group col-md-12 row">
                <label class="col-md-4 col-form-label text-md-right"></label>
                <div class="col-md-6">
                    <select name="department" class="form-control">
                        <option value="0">Please Select Department</option>
                        @foreach ($getDepartment as $department)
                        <option value=""></option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group col-md-12 row">
                <label class="col-md-4 col-form-label text-md-right"></label>
                <div class="col-md-6">
                    <select name="section" class="form-control">
                        <option value="0">Please Select Section</option>
                        @foreach ($getSection as $section)
                        <option value=""></option>
                        @endforeach
                    </select>
                </div>
            </div>

Route

 Route::resource('position', 'PositionController');



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire