Why am I getting get method not supported error(supported method : post) even though I use post method?

48 Views Asked by At

I am trying to build a user and client page with laravel I created a page that can be used to create users now I want to create a page just like that when I click those users but I keep on getting this problem. In order to map the clients to the user, I am taking the user_id as a route query

The GET method is not supported for route addClient/100. Supported methods: POST.

The problem is with the addClient method I presume I saw it just now help me modify it please

web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;
use App\Http\Controllers\ClientController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::get('/addUser',function(){
    return view('addUser');
});

Route::post('/addUser',[UserController::class, 'addUser']);

Route::get('/clients/{id}', [App\Http\Controllers\HomeController::class, 'client'])->name('clienthome');

Route::post('/addClient/{id}',function($id){
    return view('addClient',compact('id'));
});

Route::post('/add_Client',[ClientController::class, 'addClient']);

This is addclient.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Add Client</title>
    <link rel="stylesheet" href="/Bootstrap/css/bootstrap.css">
    <style>
        .container{
            height: 100vh;
            display: flex;
            align-items: center;
        }
        .mb3{
            margin-top: 5%;
        }
    </style>
</head>
<body>
    <div class="container justify-content-center">
        <form action="{{url('add_Client')}}" method="post">
            @csrf
            <div class="mb3">
                <label for="exampleFormControlInput1" class="form-label">Name</label>
                <input type="text" name = "name" class="form-control" id="exampleFormControlInput1" required>
            </div>
            <div class="mb3">
                <label for="exampleFormControlTextarea1" class="form-label">Address</label>
                <textarea class="form-control" name = "address" id="exampleFormControlTextarea1" rows="2" required></textarea>
            </div>
            <div class="mb3">
                <label for="exampleFormControlInput1" class="form-label">Country</label>
                <input type="text" name = "country" class="form-control" id="exampleFormControlInput1" required>
            </div>
            <div class="mb3">
                <label for="exampleFormControlInput1" class="form-label">Mobile</label>
                <input type="number" name = "number" class="form-control" id="exampleFormControlInput1" required>
            </div>
            <div class="mb3">
                <label for="staticEmail" class="col-sm-2 col-form-label">User_id</label>
                <div class="col-sm-10">
                    <input type="number" name = "user_id" readonly class="form-control-plaintext" id="staticEmail" value={{$id}}>
                </div>
            </div>
            <div class="mb3">
                <button type="submit" class="btn btn-outline-success">Add Client</button>
            </div>
        </form>
    </div>
    <script src="/Bootstrap/js/bootstrap.js"></script>
</body>
</html>

This url isn't even changing

The ClientController class with addClient method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\clientsData;

class ClientController extends Controller
{
    function addClient(Request $request){
        
        $errors = $request -> validate([
            'name' => 'required',
            'address' => 'required',
            'country' => 'required',
            'number' => 'required',
            'id' => 'required',
        ]);

        $clientsData = new clientsData;

        $clientsData -> name = $request -> get('name');
        $clientsData -> address = $request -> get('address');
        $clientsData -> country = $request -> get('country');
        $clientsData -> phone_no = $request -> get('number');
        $clientsData -> user_id = $request -> get('user_id');

        $clientsData -> save();
        echo "hello there";
        // return $this -> display();
    }

    function display(){
        return route('/clienthome');
    }
    
}
1

There are 1 best solutions below

4
Mit Kathrotia On

You have defined your route as post in your web.php instead of get.

Change your route like this,

Route::get('/addClient/{id}',function($id){
    return view('addClient',compact('id'));
});