Route [/math/{ $math->id }/question] not defined - php

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">

Related

Laravel I cannot get into Controller

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>

PUT method not supported for the route when trying to do an update method

This is what my code looks like
Route:
Route::put('/articles/{$article}', 'ArticlesController#update');
Controller:
public function update($id){
$article=Article::find($id);
$article ->title = request('title');
$article->excerpt=request('excerpt');
$article->body=request('body');
$article->save();
return redirect('/articles/'. $article->id);
}
Blade:
<form method="POST" action="/articles/{{$article->id}}" >
#csrf
#method('PUT')
And everytime I try to submit an update I get this:
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
I'm currently stuck on this one.
Try this
<form action="/articles/{{$article->id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
and the route
Route::put('/articles/{article}', 'ArticlesController#update');
It's better to do:
in routing
Route::put('/articles/{id}', 'ArticlesController#update')->name('articles.update');
in controller
public function update(Request $request, $id)
{
// logic
}
don't forget to use Request in controller
in blade
it's better to use naming for routes but it might be a problem in your action
<form method="POST" action="{{ route('articles.update', $article->id) }}">
simple way using blade
<form action="/articles/{{$article->id}}" method="POST">
#method('put')
#csrf
</form>

Laravel route post vs get

project is laravel 5.6. My project has 2 routes:
web.php
Route::get('testa', 'HomeController#showTestForm')->name('test');
Route::post('testa', 'HomeController#doTest');
HomeController :
public function showTestForm()
{
Log::warning('from showTestForm');
return view('test');
}
.public function doTest(Request $request)
{
Log::info('from doTest');
// return Input::all();
return view('test', [
'input' => implode(', ', Input::all()),
]);
}
test.blade.php
<form method="post" action="{{ route('test') }}">
#csrf
<input type="text" name="inputvalue">
<button type="submit" class="btn btn-primary">
merge
</button>
</form>
<div>Result</div>
#if(isset($input))
{{$input}}
#endif
Why is working post on route('test') ?
Thank you.
The reason that route('test') works even though your form is a POST request is because route() is just a helper function to generate a url and your GET and POST routes both use the same url.
You've specified in your form to make a post request and it's going to send it to the url provided (which will be the same as your GET request in this case).
Edit:
Route::post('testa', 'HomeController#doTest')->name('postTest');
and then use
<form method="post" action="{{ route('postTest') }}">
Hope this will work
It is because you'are calling the wrong route.
Change :
<form method="post" action="{{ route('test') }}">
to :
<form method="post" action="{{ url('/testa') }}">
or follow the previous answer steps (name the post route then call it)
I believe from the OP they are not understanding how the same route can accept both a GET request and a POST request.
The difference is in how the server receives the data.
Have a read: HTTP Requests

why comming from <a> route "ruta/{{$var}}/edit" and a 'action': "ruta/{{$var}} my final route is: ""ruta/{{$var}}/"ruta/{{$var}}"?

I have a view whit the next code:
<h1>Edicion</h1>
#foreach ($usuarios as $usu)
<h4>{{$usu->nombre}}</h4>
editar
#endforeach
This route:
Route::resource('/prurequests','PruebasControllers\PrurequestsController');
The method edit:
public function edit($slug)
{
$usuario = Usuario2::where('slug','=',$slug)->firstOrFail();
return view('vistaspruebas.edit', compact('usuario'));
}
In this route my URL is:
/public/prurequests/vaca/edit
The code in this view 'vistaspruebas.edit' is:
<form action="prurequests/suma" method="POST">
#method('PUT')
#csrf
<label for="nombre">ingrese nombre</label>
<input type="text" name="nombre" value="{{$usuario->slug}}">
<br />
<button type="submit" name="" value="submit">Actualiza</button>
</form>
instead of looking for this route: "prurequests/suma " Laravel look for /public/prurequests/vaca/prurequests/suma
someone knows why after the tag and call other route it removes 'edit' and change it by and other route i put here?
Please use action() helper method in your form as the following:
<form action="{{ action('Controller#method') }}" method="POST">
Or you can use the route helper method as the following:
<form action="{{ route('route_name') }}" method="POST">
Also you can use url() helper if you want to use a given path:
<form action="{{ url('path_here') }}" method="POST">

Action url for a form

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.

Categories