0
votes

how do i send a form POST method with GET route in laravel?

Route

Route::get('domain_detail/{domain_name}','domain_detailController@index');

View domain_detail folder

<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
    <div class="form-group">
        <label for="namefamily">namefamily</label>
        <input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
    </div>
    <div class="form-group">
        <label for="mobile">mobile</label>
        <input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
    </div>
    <div class="form-group">
        <label for="myprice">myprice</label>
        <input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
    </div>
    <div class="form-group">
        <input type="submit" name="send_price" class="btn btn-success" value="submit">
    </div>
</form>

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class domain_detailController extends Controller
{
    public function index($domain_name)
    {
        $domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
        if ($domain_detail_exist) {
            $domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();

            return view('domain_detail/index', ['domain_detail' => $domain_detail]);
        } else {
            return view('404');
        }
    }

    public function create()
    {
        return view('domain_detail.index');
    }
}

At the controller i didn't put any codes in the create function, but when i click on submit button in form i get this error

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: GET, HEAD.

1
Try setting the method of your form to get instead of post: <form method="get">Bart van Venrooij
@Yasin Karimian, Did my awnser solve your problem? If so mark it as correct so others that might run into the same issue can consult this post to find a possible solutionCollin

1 Answers

2
votes

Use the index function in your domain_detailController just so return the view. like this:

public function index($domain_name)
{
   return view('domain_detail.index'); 
}

create a route to return the view:

Route::get('domain_detail/','domain_detailController@index');

Then use the create function to store the domain detail like this:

public function create($domain_name)
    {
        $domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
        if ($domain_detail_exist) {
            $domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();

            return view('domain_detail/index', ['domain_detail' => $domain_detail]);
        } else {
            return view('404');
        }
    }

make a POST route like this:

Route::post('domain_detail/','domain_detailController@create');

Also take a look at the laravel best practices when it comes to naming conventions: https://www.laravelbestpractices.com/