The submit form which allows admin to change the role of user or staff, error shows Missing required parameter for [Route: updateRolePermission] [URI: admin/edit-role-permission/{id}] [Missing parameter: id] I have fighting with this issues for many hours, everyone can help thanks!!!!!
<form action="{{ route('updateRolePermission'), ['id' =>$user->id] }}" method="POST">
#csrf
<select name="roles">
<option value="user">User</option>
<option value="staff">Staff</option>
</select>
<input type="submit">
</form>
Route::group(['prefix'=>'admin', 'middleware'=>['isAdmin','auth']], function(){
Route::get('dashboard', [AdminController::class, 'index'])->name('admin.dashboard');
Route::get('role-permission', [AdminController::class, 'rolePermission'])->name('admin.rolePermission');
//it doesnt work!!!!
Route::get('edit-role-permission/{id}', [AdminController::class, 'editRolePermission'])->name('updateRolePermission');
});
function editRolePermission($id)
{
$row = DB::table('users')
->where('id',$id)
->limit(1)
->update(array('role' => 'fdas'));
return redirect()->back();
}
Change this line:
action="{{ route('updateRolePermission'), ['id' =>$user->id] }}"
to this:
action="{{ route('updateRolePermission', $user->id) }}"
First your route is GET method while your form is POST method.
For the $id, you may get it in your controller by:
$id = \Route::current()->parameter('id');
Related
I am doing roles&permissions, an action that admin can change user to staff. I am able to send id to editRolePermission function, but the role value.
function editRolePermission($id, "role value here")
{
$row = DB::table('users')
->where('id',$id)
->limit(1)
->update(array('role' => ''));
return redirect()->back();
}
<form action="{{ route('updateRolePermission', $user->id) }}" method="POST">
#method('PATCH')
#csrf
<select name="roles">
<option name ="user" value="user">User</option>
<option name= "staff" value="staff">Staff</option>
</select>
<input type="submit" onchange="this.form.submit()">
</form>
Route::patch('edit-role-permission/{id}', [AdminController::class, 'editRolePermission'])->name('updateRolePermission');
The following should do what you want. Here is the route - notice I have swapped {id} for {user} - this is the ID of the user we need to edit the role for:
Route::post("/edit-role-permission/{user}", [AdminController::class, "editRolePermission"]);
How to implement the route in your form:
#if(session()->has("message"))
{{ session("message") }}
#endif
<form action="/edit-role-permission/{{ $user->id }}" method="POST">
#csrf
<select name="roles">
<!-- ... options ... -->
</select>
<!-- submit button -->
</form>
Thanks to Laravel magic, passing the user ID as the second parameter allows us to access the user model so we can update the user easily, with a lot less code. We can use the request to get any posted values, in this instance $request->roles refers to the input named roles:
public function editRolePermission(Request $request, \App\Models\User $user)
{
$user->update(["role" => $request->roles]);
$user->save();
return redirect()->back()->with("message", "User role updated successfully");
}
I'm working with Laravel 8 to develop my forum project and in this project, I want to make an editquestion page that users can edit their questions.
So here is a button on blade that redirects user to that edit page:
<form action="{{ route('edit.question', $show->slug) }}">
<button type="submit" class="text-blue-500">Edit Question</button>
</form>
And this is the route for showing the editquestion blade:
Route::get('editquestion/{question:slug}' , [QuestionController::class, 'editQuestion'])->name('edit.question');
And this is the Controller method editQuestion() which returns a blade:
public function editQuestion(Question $slug)
{
return view('questions.editquestion',[
'slug' => $slug
]);
}
But now, whenever I click on Edit Question, I get this error:
Missing required parameter for [Route: edit.question] [URI: editquestion/{question}] [Missing parameter: question]
So what is going wrong here? How can I solve this issue?
I would really appreciate if you share your idea or suggestion about this...
Thanks in advance.
UPDATE #1:
I added input hidden of slug:
<form action="{{ route('edit.question') }}" method="POST">
#csrf
<input type="hidden" value="{{ $show->slug }}" />
<button type="submit" class="text-blue-500">Edit Question</button>
</form>
And get this error:
Missing required parameter for [Route: edit.question] [URI: editquestion/{slug}] [Missing parameter: slug]. (View: F:\xampp\htdocs\gooyanet\root\resources\views\questions\question.blade.php)
UPDATE #2:
Here is web.php routes:
Route::middleware('auth')->group(function() {
Route::get('logout', [LoginController::class, 'logout']);
Route::resource('profile' , ProfileController::class);
Route::get('ask' , [QuestionController::class, 'showForm'])->name('ask');
Route::post('ask' , [QuestionController::class, 'postForm']);
Route::get('questions/{slug}' , [QuestionController::class, 'showQuestion']);
Route::post('questions/{question}/answer' , [QuestionController::class, 'postAnswer'])->name('questions.answers');
Route::get('answers/{ans}' , [QuestionController::class, 'editAnswer'])->name('edit.answer');
Route::get('editquestion/{slug}' , [QuestionController::class, 'editQuestion'])->name('edit.question');
Route::post('editquestion/{id}' , [QuestionController::class, 'updateQuestion'])->name('update.question');
Route::post('questions/{ans}' , [QuestionController::class, 'updateAnswer'])->name('update.answer');
Route::delete('questions/{ans}' , [QuestionController::class, 'destroyAnswer'])->name('destroy.answer');
Route::delete('{id}' , [QuestionController::class, 'destroyQuestion'])->name('destroy.question');
});
From
public function editQuestion(Question $slug)
{
return view('questions.editquestion',[
'slug' => $slug
]);
}
you are injecting Question model in editQuestion(Route-model binding), so you should pass your question class instance in your form too.
<form action="{{ route('edit.question', $show) }}">
<button type="submit" class="text-blue-500">Edit Question</button>
</form>
or
<form action="{{ route('edit.question', ['question' => $show]) }}">
should work fine.
Just add slug parameter to the route function
<form action="{{ route('edit.question', ['slug' => $show->slug]) }}">
The first problem notice is incorrect syntax in Blade (options parameters should be an array in the route() function with parameter key name same as you define it in route declaration) :
Change this :
<form action="{{ route('edit.question', $show->slug) }}">
to this :
<form action="{{ route('edit.question', ['question' => $show->slug]) }}">
The other thing I think is incorrect syntax also in routing (route parameter should be only one key word, you have to choose beetween "question" or "slug", not both separated with dots) :
Route::get('editquestion/{question}' , [QuestionController::class, 'editQuestion'])->name('edit.question');
If you want to make this route parameter optional see : https://laravel.com/docs/8.x/routing#parameters-optional-parameters
Also, i don't know what your object $show->slug contain, but you should check it with a dump.
Sometimes, it happens because you haven't set the primary key and auto-increment. Please check the database and confirm your primary key column is not null.
I am using Laravel 7 to build Restaurant system
I made form that go to specific route
and I get name route but it is display error as the title
manager view
<form method="post" action="{{route('admin.distroyDish')}}" >
#csrf
<div class="form-group">
<select class="form-control" name="dish">
#for ($i = 0 ; $i < count($InetialData['dish']); $i++))
<option value="{{ $InetialData['dish'][$i]->id }}">
{{ $InetialData['dish'][$i]->name }}
</option>
#endfor
</select>
</div>
<input type="submit" name="delete_dish" value="Delete" class="btn btn-danger">
<br>
</form>
web Route file
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/manager', 'ManagerController#dashboard');
Route::post('/manager', 'ManagerController#addItem');
Route::get('/manager/{id}', 'ManagerController#deleteTable')->name('admin.deleteTable');
Route::post('/manager', 'ManagerController#destroyDish')->name('admin.distroyDish');
Route::post('/manager', 'ManagerController#addIngrediant')->name('admin.addIngrediant');
Route::get('/kitchen', 'KitchenController#index')->name('kitchen.home');
Route::get('/kitchen/{id}', 'KitchenController#submitDish')->name('kitchen.submit.dish');
Route::post('/kitchen', 'KitchenController#addIngrediant')->name('kitchen.addIngrediant');
ManagerController
private function initData()
{
$InetialData = array(
'category' => DB::table('Category')->get(),
'dish' => DB::table('Items')->get(),
'users' => DB::table('users')->get(),
'Ingrediant' => DB::table('Ingrediant')->get(),
'IngrediantHistory' => DB::table('IngrediantHistory')->get()
);
return $InetialData;
}
public function destroyDish(Request $request)
{
DB::table('Items')
->where('id', '=', $request->dish)
->delete();
return redirect('/manager')->with('InetialData' , $this->initData());
}
other route like kitchen is working
why this route is not working ??
You've got duplicate routes:
Route::post('/manager', 'ManagerController#addItem');
Route::post('/manager', 'ManagerController#destroyDish')->name('admin.distroyDish');
Route::post('/manager', 'ManagerController#addIngrediant')->name('admin.addIngrediant');
Change the endpoint structure.
As I can see you have more routes calling POST /manager. Try to use routes like resources:
GET /entities list
GET /entities/{entity} view
POST /entities create
PUT /entities/{entity} update
DELETE /entities/{entity} delete
Elegant and easy to mantain and read.
Use a pageNation to request the value of the page to the controller. But why can't any parameters reach the controller?
Route::get('/index', 'Penpal\ViewController#index')->name('penpal.index');
<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="get">
<select id="inputState" class="form-control" style="height:35px; width:80%" name="pagination" onchange="this.form.submit()">
<option value="3">#lang('penpal/component/indexMenu.twelve')</option>
<option value="4">#lang('penpal/component/indexMenu.twenty_four')</option>
<option value="5">#lang('penpal/component/indexMenu.thirty_six')</option>
</select>
</form>
public function index (Request $request){
return $request;\
}
A parameter named "menu" cannot be received from the controller.
Your <form> is using method='get', instead of method='POST' (which is used to post data to the request via a form.
You will also need to use #csrf in your blade template or you will not be able to post data:
<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="POST">
#csrf
<select id="inputState" class="form-control" style="height:35px; width:80%" name="pagination" onchange="this.form.submit()">
<option value="3">#lang('penpal/component/indexMenu.twelve')</option>
<option value="4">#lang('penpal/component/indexMenu.twenty_four')</option>
<option value="5">#lang('penpal/component/indexMenu.thirty_six')</option>
</select>
</form>
Finally, make sure that your route is a ::post() route.
Use Post method both route and form
<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="post">
Route::match(['get','post'],'/index', 'Penpal\ViewController#index')->name('penpal.index');
You haven't set any route parameters for your route and neither passing any to your controller method. And it would be a better idea to use POST then GET.
Change this to
Route::get('/index', 'Penpal\ViewController#index')->name('penpal.index');
this
Route::post('/index/{menu?}', 'Penpal\ViewController#index')->name('penpal.index');
and your form
<form action="{{ route('penpal.index', ['menu' => 'p11-c3']) }}" method="POST">
#csrf
And in your controller method you can fetch the passed parameter
public function index (Request $request, $menu){
print_r($menu);
}
My routes:
Route::group(['prefix' => 'product'], function () {
Route::get('{id}', 'ProductController#product')->where('id', '[0-9]+');
Route::post('{id}/add', 'ProductController#addToCart')->where('id', '[0-9]+');
});
From the product/{id} page i wan't to do a POST to product/{id]/add
But what is the best way to get the form action url?
Now i have:
<form method="POST" action="{{ Request::url() }}/add">
It works, but I don't like it... And there must be a beter way...
<form method="POST" action="{{ action('ProductController#addToCart') }}/add">
Given me an exception...
Missing required parameters for [Route: ] [URI: product/{id}/add]. (View: .../resources/views/product/product.blade.php)
If you dislike that, you can use route naming:
Route::post('{id}/add', 'ProductController#addToCart')
->name('product.add')
->where('id', '[0-9]+');
and then:
<form method="POST" action="{{ route('product.add', $id) }}">
where $id is a id of a element to pass.