Home > Net >  Cannot Add Data for the Second Time in Database but The First One Successful Laravel
Cannot Add Data for the Second Time in Database but The First One Successful Laravel

Time:10-25

I have 2 views file for each 2 forms in one page , i can successfully add data from first form to database and read it but when i tried in second views, it gives 419 | PAGE EXPIRED error. I use same code i used in first views to add data.

this is my routes :

Route::group([
'prefix' => 'atribut',
  'as' => 'atribut.'
], function () {

Route::group(['prefix' => 'tabHome', 'as' => 'tabHome.'], function () {
 Route::get('', [AtributDashboardController::class, 'showTab'])->name('showTab');
    
 Route::post('addDataFirst', [AtributDashboardController::class, 'addDataFirst'])->name('addDataFirst');
 Route::get('deleteDataFirst/{id}', [AtributDashboardController::class, 'deleteDataFirst'])->name('deleteDataFirst');

 Route::post('addDataSecond', [AtributDashboardController::class, 'addDataSecond'])->name('addDataSecond');
 Route::get('deleteDataSecond/{id}', [AtributDashboardController::class, 'deleteDataSecond'])->name('deleteDataSecond');
 });
});

This is my method in controller to add data:

public function __construct()
{
    $this->inpDataFirst = new inpDataFirst ();
    $this->inpDataSecond = new inpDataSecond ();
}

public function addDataFirst()
{
    $data = [
        'name' => Request()->nameForm,
        'address' => Request()->addressForm,
    ];
    $this->inpDataFirst->addData($data);
    return redirect('atribut/tabHome');
}

public function addDataSecond()
{
    $data = [
        'name' => Request()->nameForm,
        'address' => Request()->addressForm,
    ];
    $this->inpDataSecond->addData($data);
    return redirect('atribut/tabHome');
}

in first view i have form action :

{{route('frontend.atribut.tabHome.addDataFirst')}}

and in second view i have form action :

{{route('frontend.atribut.tabHome.addDataSecond')}}

the form is in same page but different views file, that's why i use return redirect

CodePudding user response:

419 error is shown when csrf token is mismatched add @csrf to your form

  • Related