I have this basic error but i cant fix it... can I have some help please ?
This is my view, and I tried with tokens #csrf and also #csrf-field and token.
I tried to write Post, post, POST.
(prat.store work well, the problem is update.)
#if(isset($ModificationMode))
<form method="post" action="{{route('prat.update', $DataPraticien ?? '')}}">
#csrf
#else
<form action="{{route('prat.store')}}" method="post">
#endif
//stuff
//stuff
/lalala
#if(isset($ModificationMode))
<button type="submit" class="btn btn-warning">Modifier Praticien</button>
#else
<button type="submit" class="btn btn-success">Ajouter Praticien</button>
#endif
my controller :
public function update(Request $request, $id)
{
$ModifPrat= Praticien::find($id);
$ModifPrat->NOM = $request->input('NOM');
$ModifPrat->ETAT_CIVIL = $request->input('ETAT_CIVIL');
$ModifPrat->NOTE = $request->input('NOTE');
$ModifPrat->NOTORIETE = $request->input('NOTORIETE');
$ModifPrat->MENBRE_ASSOCIATION = $request->input('MEMBRE_ASSOCIATION');
$ModifPrat->DIPLOME = $request->input('DIPLOME');
$ModifPrat->save();
return redirect()->route('homeAdmin', auth()->id());
}
My route is a basic resource :
Route::resource('prat', 'PratController');
NB : The variable ModificationMode is a way to use the same Page for two distinct task. I used var_dump to debug it and the variable is set well and my prat.update is detected.
Thanks ;)
If you're modifying, use this
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
you can refer to Form Method Spoofing or Resource Controllers
Check out the documentation for ResourceControllers. There is a table that explains how Laravel maps the controller methods to the request types. Basically, you need to use the #method directive (or manually add the hidden input). So, your form should look like:
<form action="{{ route('prat.update') }}" method="POST">
#method('PUT')
//...
</form>
Related
In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.
I'm doing this:
The view
#if ($message->readed != 0)
<p class="card-text message text-left">{{ $message->message }}</p>
#else
<form method="POST" action="/message/read">
#csrf
#method('PATCH')
<input type="hidden" name="message" value="{{ $message->id }}"/>
<button class="btn btn-info text-white" type="submit">
Leggi
</button>
</form>
#endif
The route in web.php
Route::patch('message/read', 'MusicianController#readMessage');
The function
public function readMessage(Request $request)
{
$message = Message::where('id', $request->id)->first();
$message->readed = 1;
$message->update();
return redirect()->back()->with('message', 'message updated');
}
But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.
Even if I had specified a patch method in routes and even in the form with #method('PATCH')
Could someone help me understand what's wrong please??
the main answer
your route is:
Route::patch('message/read', 'MusicianController#readMessage');
replace your route with following route that use for all CRUD opration:
Route::resource('message/read', 'MusicianController');
if you use ajax for submit data then replace your type and url with following:
type: "patch",
url: "{{url('message/read')}}",
if you don't use ajax than use following:
<form method="POST" action="{{url('message/read"')}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
update: after version 5.6 you can use these syntax for above functions in any blade file:
<form method="POST" action="{{url('message/read"')}}">
#csrf
#method('PATCH')
</form>
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>
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
This is my form:
<form class="form-horizontal" method="POST" action="{{ url('/categories/new') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="btn btn-default" value="Cancel" type="reset">
</form>
This is my url where my form is located: /categories/new
This is my route:
Route::get('/categories/new', 'Admin\CategoriesController#newCategory');
I want to keep the new method, so i want to check if there is a post method do smth else load the view with my form. How can I achieve this in laravel 5. I'm a newbie so all of the detailed explanations are welcomed. Thank you !
If you want to use single method for both POST and GET requests, you can use match or any, for example:
Route::match(['get', 'post'], '/', 'someController#someMethod');
To detect what request is used:
$method = $request->method();
if ($request->isMethod('post')) {
https://laravel.com/docs/master/requests#request-path-and-method
Add this to your rotes file:
Route::post('/categories/new', 'Admin\CategoriesController#someOtherFunctionHere');
I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
result page
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
Route::get('/dashboard/result/generatePDFpage', 'resultController#GeneratePDFc');
GeneratePDFc controller
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
Could someone please help me out? :-)
When you're using get method, you can do just this:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE method):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;
I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request var