submit form can not access method in controller - laravel

When I submit form in view, I can't access that in controller.
View create.blade.php
#extends('layouts.app')
#section('content')
<form method="POST" action="posts">
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
Controller PostController
public function store(Request $request)
{
return $request->all();
}
I want to access the form data, in PostController in the store method and once I click submit, it not giving the result.
I have route: Route::resource('posts', 'PostController') and I am posting to this route/url, as action="posts". In Route I have used resource and in method="post"
Does it matter? What can I do?

Looks like you are missing the action
Route::post('post', 'PostController#store');

I'm not sure but may be it's because of url and not included csrf_field(). Try changing it to:
<form method="POST" action="{{ url('posts') }}">
{{ csrf_field() }}
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
or you may use route('posts.store ') instead of url('posts')

Related

a form with the PUT method is sent with the GET method

I have 2 controller with resource clients and sumsubs.
being on the clients.show route I created a form that I would like to send to the sumsubs.update endpoint but it does not work; the form still contacts sumsubs.show
here are the routes that I have defined
here is the form
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="PUT" action="{{ route('sumsubs.update', $client->id) }}">
#csrf
<div class="form-group row">
<div class="col-sm-10">
<input type="number" class="form-control" id="candidat" name="candidat" value="{{ $client->id }}" hidden required>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success float-right"> {{ __('Recheck') }} </button>
</div>
</form>
the following is my sumbsubs controller
public function show($id)
{
return 'not ok';
}
public function update(Request $request, $id)
{
return 'ok ';
}
this is what i get when i submit the form
I would like to know where is my mistake
I read the laravel documentation for form submission and tried with url and route methods but I still get the same result
You have to use method spoofing.
Change your method in form tag to Post:
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="POST" action="{{ route('sumsubs.update', $client->id) }}">
and add this after #csrf:
#method('PUT')

The PATCH method is not supported for this route. Supported methods: GET, HEAD

<form action="{{ route('todo.edit',$todoedit->id,'edit') }}" method="POST" class="container">
#csrf
#method('PATCH')
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" value="{{$todoedit->title}}">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{$todoedit->description}}">
</div>
<button type="submit" class="btn btn-primary form-control">Update</button>
</form>
Todo Controller:
public function edit(Request $request,$id)
{
$todo=Todo::find($id);
$todo->title=$request->title;
$todo->description=$request->description;
$todo->save();
return redirect(route('todo.index'));
}
I do not know what seems to be the problem, I am doing the CRUD, everything is working but the Update part is not working, it is giving me the error
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
I have tried everything, #method('UPDATE') and PUT and everything but it does not work
Because you write your update function body in edit method in your controller. Do as below:
public function update(Request $request,$id)
{
$todo=Todo::find($id);
$todo->title=$request->title;
$todo->description=$request->description;
$todo->save();
return redirect(route('todo.index'));
}
and in your edit method just return edit view and pass $todo object to that
Method type of edit is: Get
Method type of update is: Put or Patch
You can see this types with simple run php artisan route:list in your terminal.
In Web.php May Work
Route::patch('/todo/edit/{id}', [TodoController::class, 'edit']);

The GET method is not supported for this route. Supported methods: POST in laravel ...is there something i'm missing?

blade file :
<form action="{{URL::to('/admin/sender')}}"method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Controller:
public function notificationSender(Request $request)
{
$text= request()->text;
print_r($request->input());
event(new OrderComplete($text));
return view('admin.sender');
}
Route:
Route::post('/sender','HomeController#notificationSender');
the route is a subroute for a group..is there something i'm missing?
Give space between method and route parameters here
if your laravel version is 5 then use this
<form action="{{URL::to('/admin/sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Or else you can also name the route to make it easy to pass in action parameter of form like this.
Route::post('/sender','HomeController#notificationSender')->name('sender');
Then you can pass it in form like this
<form action="{{route('sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
For GET Method (Laravel 6)
blade file
<form method="GET" action="{{ route('sender') }}" enctype="multipart/form-data" >
#csrf
<input type="text" name="text">
<input type="submit">
</form>
Write Controller
public function notificationSender(Request $request)
{
$text= $request->get('text');
echo "<pre>"; print_r($text);
event(new OrderComplete($text));
return view('admin.sender');
}
Route
Route::get('sender','HomeController#notificationSender');

How to set a route to a controller and access it via a form? [duplicate]

This question already has answers here:
Laravel form post to controller
(2 answers)
Closed 3 years ago.
I'm trying to set a route to my "MessagesController" so that a i can access it from my form, How do i set a route to the controller and access it from action form ?
This is my Form code
<form action="#" id="ajax-contact" method="GET" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
What should i write inside my web.php to set a route so i can acces my MessageController.php what should i write inside the action attribute ?
Okay not sure what you want but here is the whole thing
REQUEST(FORM) => ROUTE => CONTROLLER => RESPONSE
So you want to go from FORM to Controller you have to :
Create a controller (in App\Http\Controllers) and a method
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
// This method will handle the form
// $request is where the form data will be
public function handleForm(Request $request){
// this means dump all form values
dd($request->all());
}
}
Create a route ( In simple words, this will link between FORM & Controller method ) in web.php
// The request need to be POST to /handle-data
Route::post('handle-data','MyController#handleForm');
finally this should be your form
<form action="/handle-data" id="ajax-contact" method="POST" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
If you want to make this as an ajax call
$( "#ajax-contact" ).submit(function( event ) {
event.preventDefault();
var data = $(this).serializeArray();
$.post('/handle-data', data ).then(function(response){
console.log(response);
});
});
if you want to test the ajax way, change the dd($request->all()); to return response()->json($request->all());
That should be it :)
Edit routes inside web.php to point to submit method in MessagesController:
Route::post('submit', 'MessagesController#submit')->name('submit');
then set form action to:
<form action="{{ route('submit') }}" id="ajax-contact" method="POST" enctype="multipart/form-data">
You should POST from a form.

The POST method is not supported for this route. Supported methods: GET, HEAD

Iam trying to update a specific data using post method. After submitting the form it shows an error : The POST method is not supported for this route. Supported methods: GET, HEAD.
editpage.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<h3>Update Book</h3>
<br>
<form action="update" method="post" >
{{csrf_field()}}
#foreach($array as $fetch)
<div><input type="hidden" name="id" value="{{$fetch->id}}"></div>
<div><input type="text" name="name" class="form-control " placeholder="Bookname" value="{{$fetch->name}}" ></div><br>
<div><textarea name="content" class="form-control" rows="5" placeholder="Description" >{{$fetch->content}}</textarea></div><br>
<div><input type="text" name="author" class="form-control" placeholder="Author" value="{{$fetch->author}}"></div> <br>
<div><input type="submit" name="submit" value="Update Book" class="btn btn-success" ></div>
#endforeach
</form>
</div>
#endsection
Web Route
Route::get('/', function () {
return view('welcome');
});
Route::get('/addbook',function () {
return view('AddBook');
});
Route::post('/insert',['uses'=>'BookController#insert']);
Route::get('/delete/{id}',['uses'=>'BookController#delete']);
Route::get('/edit/{id}',['uses'=>'BookController#edit']);
``````
Route::post('/update',['uses'=>'BookController#update']);
```````
Route::get('/home',['uses'=>'BookController#index']);
Auth::routes();
There are action like update which requires the method submitted to the server url to be either PUT/PATCH (to modify the resource)
Try with this,
<form action="{{ route('book.update') }}" method="post" >
{{csrf_field()}}
{{ method_field('PUT') }}
#foreach($array as $fetch)
// ...
#endforeach
</form>
Your Route,
Route::put('update',['uses'=>'BookController#update', 'as' => 'book.update']);
Your Controller
public function update(Request $request)
{
// ...
}
Hope this helps :)

Resources