为明细表进行输入时从主表获取 ID (Laravel 8)

问题描述 投票:0回答:2

在 laravel 中我想从表主那里获取 ID。

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

我的控制器将数据保存在着陆表中:

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

return redirect()->route('landing.createlm', $landings->id);
行中,我重定向到landing.createlm.blade.php(用于将数据输入到第二个表的表单)。这有效,但我很难将数据输入到landingmetas,因为我不知道如何获取该url ID。

我的控制器将数据存储到landingmetas(详细信息表):

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

我的路线:

/*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');
php laravel eloquent foreign-keys save
2个回答
0
投票

您可以像下面这样获取该 ID。

更新您的控制器:

public function storelm(Request $request, $landingId)
{
    $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 = $landingId (here id from master table)

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

更新您的路线:

Route::post('/landinglm/{landingId}', [App\Http\Controllers\LandingController::class, 'storelm'])->name('landing.storelm');

0
投票

终于我解决了。为了解决这个问题,我只需在表单刀片中添加

<input type="text" name="landing_id" value="{{ $request->landing }}">
,我从路线中获取参数并显示在输入文本字段上,如下所示
value="{{ $request->landing }}"
,在存储功能中我只需添加此行
$lm->landing_id = $request->landing_id;
。并且不要忘记调用这个函数
$request
:

public function createlm(Request $request)
    {
        $request->route('landing');
        return view('admin.appearances.landingpages.create-field',compact('request'));
    }
© www.soinside.com 2019 - 2024. All rights reserved.