Search code examples
laraveleloquentforeign-keyssave

I am Stuck to Get ID from Master table When Doing Input for Detail Table (Laravel 8)


could you help me please, sorry i'm newbie in laravel. I want to get ID from table master but i can only sent id to URL and i don't know how to get that id to save in table detail.

i have two table, the following is the first table (master):

public function up()
    {
        Schema::create('landings', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->text('content')->nullable();
            $table->text('photo')->nullable();
            $table->timestamps();
        });
    }

then the following is the second table (detail):

public function up()
    {
        Schema::create('landingmetas', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('landing_id');
            $table->foreign('landing_id')->references('id')->on('landings')->onDelete('cascade')->onUpdate('cascade');
            $table->string('meta_key')->unique();
            $table->string('meta_value')->nullable();
            $table->timestamps();
        });
    }

this is my controller to save data in landings's table and work perfectly:

public function store(Request $request)
    {
        $landings           = new Landing();

        $landings->title    = $request->title;

        $landings->save();
        Session::flash('landing-add','Section telah dibuat.');
        return redirect()->route('landing.createlm', $landings->id);
    }

as u can see in this line return redirect()->route('landing.createlm', $landings->id); i redirect to landing.createlm.blade.php (form for input data to second table). at that point still worked as i want, but i'am struggle to input data to landingmetas 'cause i have no idea how to get that url ID. this is my controller for store data to landingmetas (detail table):

public function storelm(Request $request)
    {
        $lm     = new Landingmeta();

        $meta_key = strtolower($request->meta_key);
        $meta_key = str_replace(" ", "", $meta_key);
        $lm->meta_key   = substr($meta_key, 0, 3)."-".substr($meta_key, 3);
        $lm->landing_id = ???? (here id from master table)

        $lm->save();
        Session::flash('add-field','Field telah ditambahkan.');
        return back();
    }

and this is my route:

/*Landing page*/
    Route::get('/landings', [App\Http\Controllers\LandingController::class, 'index'])->name('landing.index');
    Route::post('/landings', [App\Http\Controllers\LandingController::class, 'store'])->name('landing.store');
    Route::get('/landings/{landing}/create', [App\Http\Controllers\LandingController::class, 'edit'])->name('landing.edit');
    Route::delete('/landings/{landing}/destroy', [App\Http\Controllers\LandingController::class, 'destroy'])->name('landing.destroy');
    /*Create Landingmetas*/
    Route::get('landings/{landing}/createfield', [App\Http\Controllers\LandingController::class, 'createlm'])->name('landing.createlm');
    Route::post('/landinglm', [App\Http\Controllers\LandingController::class, 'storelm'])->name('landing.storelm');

Solution

  • Finally i solved. To solve this i just add <input type="text" name="landing_id" value="{{ $request->landing }}"> in form blade, i take parameter from route and displaying on input textfield like this value="{{ $request->landing }}" and at store function i just add this line $lm->landing_id = $request->landing_id;. And don't forget this function to call $request:

    public function createlm(Request $request)
        {
            $request->route('landing');
            return view('admin.appearances.landingpages.create-field',compact('request'));
        }