你能幫我嗎,對不起,我是laravel的新手。我想從表主那里獲取 ID,但我只能將 ID 發送到 URL,我不知道如何獲取該 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,因為我不知道如何獲取該網址 ID。這是我用于將資料存盤到 landmetas(詳細資訊表)的控制器:
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');
uj5u.com熱心網友回復:
您可以像下面那樣獲得該 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');
uj5u.com熱心網友回復:
最后我解決了。為了解決這個問題,我只是<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'));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/380509.html
上一篇:在php中授予角色訪問權限?
