noblemfd
1 month ago
116
2
Laravel

Master Detail Form with Just two tables

Posted 1 month ago by noblemfd

I am developing an appraisal management system using Master Detail Form with just three tables:

  1. Goals

  2. KPI

  3. Goal Types

Models

Goal

protected $fillable = [
    'goal_name',
    'goal_description',
];

public function goalType()
{
    return $this->belongsTo('App\Models\GoalType','goal_type_id');
}

KPI

protected $fillable = [
'goal_id
    'goal_type_id',
    'kpi_description',
];

GoalController

public function create()
{
    $goaltypes = GoalType::all();
    return view('goals.create');
}

Goal Type

protected $fillable = [
              'goal_type_name'
          ];

public function store(StoreGoalRequest $request)
{
    
    $goaltype = GoalType::create([
        'goal_name'                 => $request->goal_name,
         'goal_description'         => $request->goal_description,
        'created_by'                => Auth::user()->id,
        'created_at'                => date("Y-m-d H:i:s"),
        'is_active'                 => 1,
    ]);
    Session::flash('success', 'Appraisal Goal is created successfully');
    return redirect()->route('goals.index');
}

One Goal will have many KPI. The goal_name, goal_type_name and goal_description are entered only once at an instance. goal_type_name is a dropdown derived from goal_type_id.

I want to create a master detail form, whereby there will be a add button that will add kpi_description (text box) several times.

When save button is clicked:

  1. goal_name and goal_description will be saved in goals table

  2. goal_type_id from the dropdown, goal_id from goals table and kpi_description will be saved in the kpi table

  3. nothing is saved in goal_type table. It only supplies the goal_type_id and goal_type_name for the dropdown

  4. goals table is the main controller.

How do I modify my model and controller, and also write my view to achieve this?

Thank you

Please sign in or create an account to participate in this conversation.