cloud soft
cloud soft

Reputation: 195

How to make form submit with post in laravel 5.4

I am learning laravel 5.4 framework.

In web.php:

Route::post('form-submit',[

'uses' => 'Admincontroller@formSubmit',
'as' => 'f.submit',

]);

Admincontroller is:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Request;
use App\Http\Controllers\Controller;
use App\Customer;

class Admincontroller extends controller{

public function index()
{
//echo"Index method";
return view('Welcome');
}
public function formSubmit()
{
    echo"HTML form submit";
} 
?>

xyz.blade.php is:

@extends('layout/test-data2')
@section('content')
{!! Form::open(['url' => 'form-submit'])  !!}
{!! Form::text('field_one') !!}
    {!! Form::submit('submit') !!}

{!! Form::close() !!}
@endsection

After run form-submit, a form should be open and if click on button then message "HTML form submit" should be echo.

But when I run link, then post is not working and give error:

When I run url admin3, then got error:

(1/1) MethodNotAllowedHttpException
in RouteCollection.php line 251
at RouteCollection->methodNotAllowed(array('POST'))
in RouteCollection.php line 238
at RouteCollection->getRouteForMethods(object(Request), array('POST'))
in RouteCollection.php line 176
at RouteCollection->match(object(Request))
in Router.php line 546

But when I use get it shows only text message: HTML form submit

Upvotes: 0

Views: 4945

Answers (1)

user7747472
user7747472

Reputation: 1952

That syntex is correct, there is nothing wrong, Thing is that you can not simply run a POST url, that will be consider as GET request.

For POST to work you need to specify that its a post request.Like in the form we specify that

<form method="post" action=<URL HERE>> </form>

You see we mention that its a post request. But if you simply try to access www.example.com/admin3 that will be consider as GET request as a result of which you are getting that error.

And one more thing, For post request in the controller, add Request $request to capture the post data. Something like this

public function postindex(Request $request)
{
   $data = $request->all(); //This will give you the data of all post value
    echo"Index method";
    //return view('Welcome');
}

Dont forget to import the Request as well.

Upvotes: 1

Related Questions