Home > OS >  Laravel 8 - Routing Problem - Missing Required Parameter
Laravel 8 - Routing Problem - Missing Required Parameter

Time:11-30

I have routing problem, that I can't seem to fix.

So first: Here is a dynamicly created buttons in my "languages.blade.php"

<a href="edit-keywords/{{ $language->language_code }}" class="btn btn-success btn-sm"><i class="fa fa-language"></i>&nbsp; Edit Keywords</a>

Here is routing lines in my "web.php"

 Route::get('/admin/edit-keywords/{code}', [App\Http\Controllers\Admin\LanguageController::class, 'edit_keywords'])->name('admin.edit-keywords');
 Route::post('/admin/edit-keywords', [App\Http\Controllers\Admin\LanguageController::class, 'save_keywords'])->name('admin.save_keywords');

Here is public function in my "LanguageController.php"

public function edit_keywords($code){

   $language['view'] = Languages::getLanguage($code);
   $keyword['view'] =  Languages::getKeyword($code);
   $data = array('language' => $language, 'keyword' => $keyword);

   return view('admin.edit-keywords')->with($data);

}

And here is 2 functions in my languages model

  public static function getLanguage($code){

    $value=DB::table('languages')->where('language_code', '=', $code)->first();
    return $value;

  }

  public static function getKeyword($code){

     $value=DB::table('keywords')->where('language_code', $code)->orderBy('keyword_id', 'asc')->get();
     return $value;

  }

When I click on the button it brings me to link: http://localhost/admin/edit-keywords/LV so for routing it's all right.

But I get this error everytime and for every language:

Illuminate\Routing\Exceptions\UrlGenerationException

Missing required parameter for [Route: admin.edit-keywords] [URI:admin/edit-keywords/{code}]

[Missing parameter: code]. (View: C:\xampp\htdocs\resources\views\admin\edit-keywords.blade.php)

http://localhost/admin/edit-keywords/LV

Edit:

Here it works:

Route::get('/admin/edit-language/{language_code}', [App\Http\Controllers\Admin\LanguageController::class, 'edit_language'])->name('admin.edit-language');
Route::post('/admin/edit-language', [App\Http\Controllers\Admin\LanguageController::class, 'update_language'])->name('admin.edit-language');

CodePudding user response:

I advice you to use the route name as mentionned in the official documentation, and add your url parameter like this :

<a href="{{ route('admin.edit-keywords', $language->language_code) }}" class="btn btn-success btn-sm"><i class="fa fa-language"></i>&nbsp; Edit Keywords</a>

CodePudding user response:

I really appreciate everyone's help.

How it seams, since I upgraded to Laravel 8, there is different routing ways.

I found problem: It was in "edit-keywords.blade.php"

I had to change

<form action="/admin/edit-keywords/" method="post" id="setting_form" enctype="multipart/form-data">

To

 <form action="/admin/edit-keywords/{code}" method="post" id="setting_form" enctype="multipart/form-data">

And route also

  Route::post('/admin/edit-keywords/', [App\Http\Controllers\Admin\LanguageController::class, 'save_keywords'])->name('admin.save_keywords');

Changed to

  Route::post('/admin/edit-keywords/{code}', [App\Http\Controllers\Admin\LanguageController::class, 'save_keywords'])->name('admin.save_keywords');
  • Related