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.
Related
The storeClientRequest Cannot be triggered, is there any mistake here ? When I hit submit, the page shows 404 Not found
Form
<form class="needs-validation" novalidate method="POST" action="store-client-request/{{ Auth::user()->id }}">
#csrf
//////content
</form>
Route
Route::group(['prefix'=>'user', 'middleware'=>['isUser','auth']], function(){
Route::post('store-client-request/{user}', [UserController::class, 'storeClientRequest']);
});
Controller
function storeClientRequest(User $user)
{
dd('hi');
return redirect()->back()->with("message", "Create request successfully");
}
Add a name to your route :
Route::post('store-client-request/{user}', [UserController::class, 'storeClientRequest'])->name("store.client.request");
Make sure you run
php artisan optimize
Reference your route by name in the opening form tag :
action="{{ route('store.client.request', ['user' => Auth::user()->id]) }}">
That way, it doesn't matter (a) what the route prefix is (that it looks like you've forgotten to include) or (b) if the address to the route changes later down the line - the {{ route() }} blade directive will always pull in the correct URL, along with the relevant parameters.
As I see through you code: your full route is /user/store-client-request/{user}
Therefore in you action you should put
<form class="needs-validation" novalidate method="POST" action="/user/store-client-request/{{ Auth::user()->id }}">
#csrf
//////content
In your Route there is /user/store-client-request/{user}
So Add this in your Action
<form class="needs-validation" novalidate method="POST" action="/user/store-client-request/{{ Auth::user()->id }}">
</form>
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');
I was working on a project earlier on and ran into this error
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [/math/{ $math->id }/question] not defined.
Here's my route:
Route::post('/math/{math}/question', [App\Http\Controllers\QuestionController::class, 'store'])->name('/math/{math}/question')->middleware('auth');
Blade File Route:
<form action="{{ route('/math/{ $math->id }/question') }}" method="post">
Controller:
public function create(Math $math)
{
return view('question.create', compact('math'));
}
What am I doing wrong?
1st, route() uses the ->name() parameter.
2nd, math/{math}/question is not a good route name.
Change your name to something sensical, like math_question, then fix your code:
Route::post('/math/{math}/question', [App\Http\Controllers\QuestionController::class, 'store'])->name('math_question')->middleware('auth');
Then:
<form action="{{ route('math_question', ['math' => $math->id]) }}" method="post">
Route::post('/math/{math}/question', [App\Http\Controllers\QuestionController::class, 'store'])->name('mathQuestion')->middleware('auth');
<form action="{{ route('mathQuestion', ['math' => $math->id]) }}" method="post">
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 trying to set a Password reminder in restful way. (Following this tutorial http://laravel.com/docs/4.2/security#password-reminders-and-reset but trying to do it in restful way)
The route looks like this,
Route::group(array('prefix' => 'api/v1'), function(){
Route::resource(
'password', 'RemindersController',
array(
'only' => array('store', 'show', 'update')
)
);
});
RemindersController starts as,
public function update()
{
}
The password reset url is
http://192.x.x.x:8000/api/v1/password/3adb8b0454144ef5aeaa333faa5c575bd833e03d
From this url loading reset.blade as follows,
<form action="{{ action('RemindersController#update') }}" method="PUT"
...
<input type="submit" value="Reset Password"> </form>
But when loading this page, the form action seems to have some issues, the action url does not seem to be right.
<form action="http://192.x.x.x:8000/api/v1/password/%7Bpassword%7D" method="PUT">
What is the right way to provide action property in the form for this? How can I pass the password reset details to 'update' method in Reminder controller?
In the mentioned tutuorial it is like
action="{{ action('RemindersController#postReset') }}" method="POST"
What will change when using the restful resource way?
Got it right by following the suggestion from this site with the following modification,
<form action="{{ URL::to('api/v1/password/update') }}" method="POST">
<input name="_method" type="hidden" value="PUT">