phplaraveleloquentforeign-keyssave

Get ID from Master table When Doing Input for Detail Table (Laravel 8)


in laravel I want to get ID from 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();
        });
    }

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();
        });
    }

my controller to save data in landings's table:

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);
    }

in line return redirect()->route('landing.createlm', $landings->id); i redirect to landing.createlm.blade.php (form for input data to second table). that worked but i struggle to input data to landingmetas because i have no idea how to get that url ID.

my controller to 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();
    }

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'));
        }