Binding form action to controller method - php

I am trying to bind form action(which has id related to a model/table record) to controller method.
My web.php has
Route::post('/rejectControlTransfer/{id}', 'ControlTransferController#rejectControlTransfer')->name('controltransfers.rejectTransfer');
My form has
<form id='form_process_rejectControl' action="{{route('controltransfers.rejectTransfer', [$controlTransferId])}}" method="POST" style="display: inline;">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
And my controller has
public function rejectControlTransfer(Request $request, ControlTransfer $controlTransfer)
{
dd($controlTransfer->id);
}
I am trying to bind ControlTransfer $controlTransfer with the actual id passed so that when I try get value of $controlTransfer->id or $controlTransfer->name would give me their values.
Current I am not getting any value.

If you are using id in the route '/rejectControlTransfer/{id} then you can only access it through $id variable in your controller, which is a raw variable int.
Besides that, your action's 'route' function is not used correctly, you need to put 'id' as your key, like:
route('controltransfers.rejectTransfer', ['id' => $controlTransferId])
However, if your ControlTransfer is a model, you can use Model Binding. By:
Route::post('/rejectControlTransfer/{controlTransfer}', 'ControlTransferController#rejectControlTransfer')->name('controltransfers.rejectTransfer');
<form id='form_process_rejectControl' action="{{route('controltransfers.rejectTransfer', ['controlTransfer' => $controlTransferId])}}" method="POST" style="display: inline;">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
public function rejectControlTransfer(Request $request, ControlTransfer $controlTransfer)
{
dd($controlTransfer->id);
}
Disclaimer: Above code is untested.

Related

laravel 8 , How to display data from a FORM with post method?

my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image
Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.

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>

The POST method is not supported for this route()

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>

Laravel parameter issue

Missing argument 2 for App\Http\Controllers\Company\OrderController::store()
I got this error in my store controller, my form is passing 2 parameters but the second one is not found.
Route: Route::resource('order', 'OrderController');
$company gets converted to a model in the controller.
The form:
<form class="form-horizontal" role="form" method="POST" action="{{action('Company\OrderController#store', [$company,$orderid])}}">
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Any ideas?
Thanks!
If you created store route with Route::resource() it doesn't expect any parameters and should look like this:
public function store(Request $request)
So, you need to pass data using hidden inputs, like:
{!! Form::hidden('data', 'some data') !!}
And then get data in controller with:
$data = $request->data;
You should specify the key-value pair like this:
['company_id' => $company->id, 'order_id' => $order->id]
So your form would look like:
<form action="{{ action('Company\OrderController#store', ['company_id' => $company->id, 'order_id' => $order->id]) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Hope this helps!

How to get data transfer from the form into database Laravel 5

I'm trying to transfer data from the form to overwrite the content database .But get the error.
It is my route,method and form.
Route::get('edit/{id}','JokeController#edit');
public function edit(JokeModerateRequest $request,$id) {
$joke = Joke::findOrFail($id);
return view('jokes.edit', [ 'joke' => $joke ]);
}
<form action="/update" method="post">
<input type="text" value="{{ $joke -> content}}" name="body">
<input type="submit" value="Save">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
But when I try use next route and method
Route::post('update/{id}','JokeController#update');
function update(JokeModerateRequest $request,$id)
$joke = Joke::findOrFail($id);
$joke->content = $request -> get('content');
$joke->save();
return back();
I have next error
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 145:
The problem lies here:
<form action="/update" method="post">
This will redirect to /update route (which you haven't defined) instead of /update/id. Those two are different routes.
To fix this, change action of the form to:
<form action="/update/{{ $joke->id }}" method="post">

Categories