skip to Main Content

I am very new to Laravel. I am currently doing a Laravel project where a person can submit a proposal. The Laravel version I’m using is Laravel Framework 8. I divided the proposal form into 7 pages. I also have 7 tables in the database. So the data of each page will be saved into each of the tables. 1 page = 1 table. This means I also have 7 models. In conclusion, I have 7 views, 7 controllers, 7 models, and 7 tables (if this is not a good practice please tell me)

Here is an example of my code

Route

web.php

Route::get('/form/details', [AppHttpControllersLectFormStep1Controller::class, 'createStepOne'])->name('form.create.step.one');
Route::post('/form/details', [AppHttpControllersLectFormStep1Controller::class, 'postCreateStepOne'])->name('form.create.step.one.post');
Route::get('/form/learning-outcomes', [AppHttpControllersLectFormStep2Controller::class, 'createStepTwo'])->name('form.create.step.two');
Route::post('/form/learning-outcomes', [AppHttpControllersLectFormStep2Controller::class, 'postCreateStepTwo'])->name('form.create.step.two.post');
...

Model

Step1.php

class Step1 extends Model {
    use HasFactory;
    public $table = 'forms';

    protected $fillable = [
        'title',
        'code',
        'creditvalue',
        'mqflevel',
        'affectedbatch',
        'kulliyyah',
        'department',
        'synopsis',
        'classification',
        'prerequisite',
    ];
}

Step2.php

class Step2 extends Model {
    use HasFactory;
    public $table = 'proposal_learning_outcome';
    public $timestamps = false;

    protected $fillable = [
        'proposal_id',
        'lo_id',
        'outcomes',
        'bloom_c',
        'bloom_a',
        'bloom_p',
        'ki',
        'po',
    ];
}

Controller

Step1Controller.php

class Step1Controller extends Controller {
    public function createStepOne(Request $request) {
        return view('form.step1');
    }

    public function postCreateStepOne(Request $request) {
        $validatedData = $request->validate([
            'title' => 'required',
            'code' => 'required|unique:forms',
            'creditvalue' => 'required|numeric',
            'mqflevel' => 'required|numeric',
            'affectedbatch' => 'required',
            'kulliyyah' => 'required',
            'department' => 'required',
            'synopsis' => 'required',
            'classification' => 'required',
            'prerequisite' => 'required',
        ]);

        $step1 = new Step1;
        $step1->title=$request->input('title');
        $step1->code=$request->input('code');
        $step1->creditvalue=$request->input('creditvalue');
        $step1->mqflevel=$request->input('mqflevel');
        $step1->affectedbatch=$request->input('affectedbatch');
        $step1->kulliyyah=$request->input('kulliyyah');
        $step1->department=$request->input('department');
        $step1->synopsis=$request->input('synopsis');
        $step1->classification=$request->input('classification');
        $step1->prerequisite=$request->input('prerequisite');
        $step1->created_by=Auth::user()->username;
        $result = $step1->save();

        return redirect()->route('form.create.step.two');
    }
}

Step2Controller.php

class Step2Controller extends Controller {
    public function createStepTwo(Request $request) {
        return view('form.step2');
    }

    public function postCreateStepTwo(Request $request) {
        $validatedData = $request->validate([
            'proposal_id' => 'required',
            'lo_id' => 'required',
            'outcomes' => 'required',
            'bloom_c' => 'required',
            'bloom_a' => 'required',
            'bloom_p' => 'required',
            'ki' => 'required',
            'po' => 'required',
        ]);

        foreach($request->lo_id as $key=>$lo_id) {
            $data = new Step2();
            $data->proposal_id=$request->input('proposal_id');
            $data->lo_id=$lo_id;
            $data->outcomes=$request->outcomes[$key];
            $data->bloom_c=$request->bloom_c[$key];
            $data->bloom_a=$request->bloom_a[$key];
            $data->bloom_p=$request->bloom_p[$key];
            $data->ki=$request->ki[$key];
            $data->po=$request->po[$key];
            $data->save();
        }

        return redirect()->route('form.create.step.three');
    }
}

View

step1.blade.php

<form action="{{ route('form.create.step.one.post') }}" method="POST">
    @csrf
    <div class="card">
        Step 1: Basic Info 
    </div>
    <div class="card-body">
        @if ($errors->any())
                            <div class="alert alert-danger">
                                <ul>
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif

                        <div class="form-group">
                            <label for="title">Course Title</label>
                            <input type="text" name="title" class="form-control text-lg" id="title" placeholder="">
                            </div>
                            <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                <label for="code">Course Code</label>
                                <input type="text" name="code" class="form-control text-lg" id="code" placeholder="">
                                </div>
                                <div class="form-group">
                                <label for="creditvalue">Credit Value</label>
                                <input type="number" name="creditvalue" class="form-control text-lg" id="creditvalue" placeholder="">
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                <label for="mqflevel">MQF Level</label>
                                <input type="number" name="mqflevel" class="form-control text-lg" id="mqflevel" placeholder="">
                                </div>
                                <div class="form-group">
                                <label for="affectedbatch">Affected Batch</label>
                                <input type="text" name="affectedbatch" class="form-control text-lg" id="affectedbatch" placeholder="">
                                </div>
                            </div>
                            </div>

                            <div class="form-group">
                            <label for="kulliyyah">Centre of Studies</label>
                            <select type="text" class="form-control text-lg" name="kulliyyah" id="kulliyyah">
                                <option value="Kulliyyah of Information and Communication Technology">Kulliyyah of Information and Communication Technology</option>
                                <option value="Kulliyyah of Education">Kulliyyah of Education</option>
                                <option value="Kulliyyah of Engineering">Kulliyyah of Engineering</option>
                                <option value="Kulliyyah of Architecture and Environmental Design">Kulliyyah of Architecture and Environmental Design</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="department">Department/Unit</label>
                            <select type="text" class="form-control text-lg" name="department" id="department">
                                <option value="Department of Computer Science">Department of Computer Science</option>
                                <option value="Department of Information System">Department of Information System</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="synopsis">Course Synopsis</label>
                            <textarea type="text" class="form-control text-lg" name="synopsis" rows="5" id="synopsis" placeholder=""></textarea>
                        </div>

                        <div class="form-group">
                            <label for="classification">Course Classification within the Curriculum</label>
                            <input type="text" class="form-control text-lg" name="classification" id="classification" placeholder="eg: CSC 1305">
                        </div>

                        <div class="form-group">
                            <label for="prerequisite">Prerequisite(s) (if any)</label>
                            <input type="text" class="form-control text-lg" name="prerequisite" id="prerequisite" placeholder="">
                        </div>

                    </div>
                    <div class="card-footer text-right">
                        <button type="submit" class="btn btn-primary">Next</button>
                    </div>
                </div>
            </form>

step2.blade.php

<form action="{{ route('form.create.step.two.post') }}" method="POST">
                @csrf
                <div class="card">
                    
                    Step 2: Course Learning Outcomes</div>

                    <div class="card-body">
                        @if ($errors->any())
                            <div class="alert alert-danger">
                                <ul>
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif

                        <div class="form-group">
                            <label for="proposal_id">Proposal ID</label>
                            <input type="text" class="form-control text-lg" name="proposal_id" id="proposal_id" placeholder="">
                        </div> 


                        <div class="table-responsive">
                            <form method="post" id="dynamic_form">
                                <span id="result"></span>
                                <table class="table table-bordered" id="user_table">
                                    <thead>
                                        <tr class="table-active">
                                            <th rowspan="2">No.</th>
                                            <th rowspan="2" class="w-50">Outcomes</th>
                                            <th colspan="3" class="w-25">Bloom's Taxonomy</th>
                                            <th rowspan="2">Soft skills (KI)</th>
                                            <th rowspan="2">Programme Outcomes (PO)</th>
                                            <th rowspan="2"><a href="javascript:;" class="btn btn-info addRow">Add</a></th>
                                        </tr>
                                        <tr  class="table-active">
                                            <th>C</th>
                                            <th>A</th>
                                            <th>P</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <tr>
                                        <td><input class="form-control text-lg" name="lo_id[]"/></td>
                                        <td><textarea class="form-control text-lg" name="outcomes[]"></textarea></td>
                                        <td><input class="form-control text-lg" name="bloom_c[]"/></td>
                                        <td><input class="form-control text-lg" name="bloom_a[]"/></td>
                                        <td><input class="form-control text-lg" name="bloom_p[]"/></td>
                                        <td><input class="form-control text-lg" name="ki[]"/></td>
                                        <td><input class="form-control text-lg" name="po[]"/></td>
                                        <td><a href="javascript:;" class="btn btn-danger deleteRow">Remove</a></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </form>
                        </div>

                    </div>
                    
                    <div class="card-footer">
                        <div class="row">
                            <div class="col-md-6 text-left">
                                <a href="{{ route('form.create.step.one') }}" class="btn btn-danger pull-right">Previous</a>
                            </div>
                            <div class="col-md-6 text-right">
                                <button type="submit" class="btn btn-primary ">Next</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

and the other 5 steps are basically like that.

My current problem is how can I get the proposal_id of the first form (step1) as a reference to the next 6 forms. The proposal_id is used as a foreign key for other tables. The proposal_id is auto-incremented in the table.

The reason I divided them into many tables is that some parts of the form are in table format as shown in step2.blade.php. The image below a reference.

Form example

For now, as you can see in the code, my current solution is to manually input the proposal_id in each form (which is not practical because they would not know the proposal_id in the first place). My current code has no issue. It can be saved into the database as normal.

How can I solve this issue? Or is there another way to fill up this form? Thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    The answer was helpful but not actually what I asked (sorry if my question was not clear). I found the answer to my question.

    I wanted to get the proposal_id from the first form. I called the lastest added row in the table like this:

    $proposal_id = DB::table('forms')
                ->select('proposal_id')
                ->orderBy('proposal_id', 'desc')
                ->limit(1)
                ->value('proposal_id');
    

    I added this code in the Step1Controller after

    $result = $step1->save();

    And then I save it into session like crazy4dev suggested:

    $request->session()->put('proposal_id', $proposal_id);
    
    return redirect()->route('form.create.step.two', compact('proposal_id'));
    

    I can access the proposal_id in the Step2Controller with:

    $proposal_id = $request->session()->get('proposal_id');
    

  2. You have two solution, the first I see is to pass the proposal_id as a parameter of your route (see below).

    Route::get('/form/learning-outcomes{proposal_id}', [AppHttpControllersLectFormStep2Controller::class, 'createStepTwo'])->name('form.create.step.two');
    

    The second is to save it in your session variable and you can acess it throught your form

    
    session()->put('proposal_id', $proposal_id)
    

    To get it in your second form use :

    session()->get('proposal_id')
    

    Update
    Make sure to add this line in your migration file:

    $table->id('proposal_id')
    

    where the proposal_id is your primary key autoincrement. By default if you don’t put anything as id parameter, it will be id in your column table.

    $table->id()//COLUMN TABLE WILL CONTAINT id as name
    $table->id('proposal_id')//COLMUN TABLE WILL CONTAINT proposal_id as name
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search