【问题标题】:I am Stuck to Get ID from Master table When Doing Input for Detail Table (Laravel 8)输入详细信息表时,我无法从主表中获取 ID(Laravel 8)
【发布时间】:2022-01-17 11:11:04
【问题描述】:

你能帮帮我吗,对不起,我是 laravel 的新手。我想从 table master 获取 ID,但我只能将 id 发送到 URL,我不知道如何获取该 id 以保存在 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();
        });
    }

这是我的控制器,用于将数据保存在着陆表中并完美运行:

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

【问题讨论】:

    标签: laravel eloquent foreign-keys save


    【解决方案1】:

    您可以像下面这样获取该 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');
    

    【讨论】:

    • 没用,我只想获取ID 24 http://example.id/admin/landings/24/createfield 存储详细表。
    • @N.Arifin 如果您使用此路由,则应将 $landingId 作为参数添加到 createlm() 方法中,或者如您在上面回答的那样,您必须使用输入传递它。两种方法都是正确的。祝你好运!
    • 非常感谢
    【解决方案2】:

    终于解决了。为了解决这个问题,我只需在表单刀片中添加<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'));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2022-11-27
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多