The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel 8 error used, what I am doing wrong here?
My form file name -- insertemp.blade.php
<form method="POST" action="/showemployee">
#csrf
<div class="form-group" style="margin-left: 30px">
<label for="id">ID</label>
<input class="form-control" type="number" name="eid" id="empid"><br>
</div>
</form>
My web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('/showemployee',[EmployeeController::class,'show']);
Route::post('/showemployee',[EmployeeController::class,'store']);
My controller file -- EmployeeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
class EmployeeController extends Controller
{
public function show()
{
$emp = Employee::all();
return view('showemployee',['employees' => $emp]);
}
public function store()
{
echo "I am in store";
error_log(request('eid'));
return redirect('/');
}
}
Route::post('/showemployee', [EmployeeController::class,'store'])->name('form.post');
and
<form method="POST" action="{route('form.post')}}" >
#csrf
<div class="form-group" style="margin-left:30px">
<label for="id">ID</label>
<input class="form-control" type="number" name="eid" id="empid">
</div>
</form>
if still not works
check with :
php artisan route:list
Related
Thanks in advance for taking the time to read throw the question.
so I have a form in a blade view where a user can add a name, a description, and upload an image but the data is not being passed to the database
The blade view:
<div class="container">
<form action="/list">
<div class="form-group">
<label for="Name">Name:</label>
<input type="Name" class="form-control" id="Name" placeholder="Enter Name" name="Name">
</div>
<div class="form-group">
<label for="description">description:</label>
<input type="description" class="form-control" id="description" placeholder="Enter description" name="description">
</div>
<p>image:</p>
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="image" name="image">
<label class="custom-file-label" for="image">Choose file</label>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<script>
$(".custom-file-input").on("change", function() {
var image = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(image);
});
</script>
store function in controller :
<?php
namespace App\Http\Controllers;
use App\Models\Realestate;
use Illuminate\Http\Request;
class RealestatesController extends Controller
{
//
function store(Request $request){
$realestate =new Realestate();
$realestate->name= $request->name;
$realestate->description= $request->description;
$name=$request->file('image')->getClientOriginalName();
$request->file('image')->storeAs('public/storage',$name);
$realestate->image=$name;
$realestate->save();
return redirect('/list');
}
}
The migration which I had used:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Realestates extends Migration
{
public function up()
{
Schema::create('realestates', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->string('image');
$table->timestamps();
});
}
}
and at last the route which I'm using is:
Route::post('/list',[RealestatesController::class, 'store']);
did I miss anything in the previously mentioned code?
You're missing 2 things on your form:
method='POST' -- this tells the form to actually submit the form as a POST request and not a GET request
#csrf -- All Laravel forms must have a CSRF token to be accepted, unless it is explicitly bypassed in the middleware or submitted via GET, such as a search form
edit
Also for file uploads, you need to add enctype="multipart/form-data" to your form.
I have a problem with save-aboutus page. I am pretty new to Laravel, I don't know how can I fix this.
I get this error:
The GET method is not supported for this route. Supported methods:
PUT.
routes(web.php):
Route::group(['middleware' => ['auth','admin']], function() {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('/role-register',[App\Http\Controllers\Admin\DashboardController::class, 'registered']);
Route::get('/role-edit/{id}',[App\Http\Controllers\Admin\DashboardController::class, 'registeredit']);
Route::put('/role-register-update/{id}',[App\Http\Controllers\Admin\DashboardController::class, 'registerupdate']);
Route::delete('/role-delete/{id}',[App\Http\Controllers\Admin\DashboardController::class, 'registerdelete']);
Route::get('/abouts',[App\Http\Controllers\Admin\AboutusController::class, 'index']);
Route::post('/save-aboutus', [App\Http\Controllers\Admin\AboutusController::class, 'store']);
});
Controller:
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts;
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$descreption = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','Success');
}
}
and blade.php
<form action="/save-aboutus" method="POST">
{{ csrf_field() }}
<div class="modal-body">
<div class="form-group">
<label for="recipient-name" class="col-form-label">Title:</label>
<input type="text" name="title" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Sub-title</label>
<input type="text" name="subtitle" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Description</label>
<textarea class="form-control" name="description" id="message-text"></textarea>
</div>
</div>
</form>
You just need to add a GET route if you want to access it with GET method:
Route::get('/save-aboutus', /* handler */);
Error while navigating from edit/id to home and other pages. Navingation stucks at edit/id and changes to edit/add or edit/home instead of resto/home . Also while hiting update getting error POST method is not supported
This is my route-web code
Route::get('/', function () {
return view('welcome');
});
Route::get('home','RestoController#index');
Route::get('list','RestoController#list');
Route::post('add','RestoController#add');
Route::view('add','add');
Route::get('delete/{id}','RestoController#delete');
Route::get('edit/{id}','RestoController#edit');
This is my edit code
#extends('layout')
#section('content')
<div class="col-sm-6">
<h1>Edit Restaurant</h1>
<form action="edit" method="post">
#csrf
<div class="form-group">
<label>Name</label>
<input type="hidden" name="id" value="{{$data->id}}">
<input type="text" name="name" class="form-control" value="{{$data->name}}" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Email ID</label>
<input type="text" name="email" class="form-control" value="{{$data->email}}" placeholder="Enter Email ID">
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control" value="{{$data->address}}" placeholder="Enter Address">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
#stop
and This is my Controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Restaurant;
use Session;
class RestoController extends Controller
{
//
public function index()
{
return view('home');
}
public function list()
{
$data= Restaurant::all();
return view('list',["data"=>$data]);
}
function add(Request $req)
{
// return $req->input();
$resto = new Restaurant;
$resto->name=$req->input('name');
$resto->email=$req->input('email');
$resto->address=$req->input('address');
$resto->save();
$req->session()->flash('status','Restaurant added successfully');
return redirect('list');
}
function delete($id, Request $req)
{
Restaurant::find($id)->delete();
Session::flash('status','Restaurant deleted successfully');
return redirect('list');
}
function edit($id)
{
$data= Restaurant::find($id);
return view('edit',["data"=>$data]);
}
function update(Request $req)
{
$resto = Restaurant::find($req->input('id'));
$resto->name=$req->input('name');
$resto->email=$req->input('email');
$resto->address=$req->input('address');
$resto->save();
$req->session()->flash('status','Restaurant updated successfully');
return redirect('list');
}
}
please help me and guide me where i am lacking ? also, while hitting update got error - the POST method is not supported, Thanks
My View:
<form action="/AddJuiceForm" method="POST">
#csrf;
<div class="form-group">
<label for="FruitName">Fruit Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Fruit Name"
name="fruitname">
</div>
<div class="form-group">
<label for="JuiceName">Juice Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Juice Name"
name="juicename">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My Routes/web.php:
Route::post('AddJuiceForm','JuiceController#insertjuice ');
My Controller:
<?php
namespace App\Http\Controllers;
use App\JuiceModel;
use Illuminate\Http\Request;
class JuiceController extends Controller
{
public function insertjuice()
{
dd(request()->all);
}
}
Result: I am getting 'null' as output
Help me where I am going wrong?
Inject the Illuminate\Http\Request object into the insertjuice method and then call the all() method on the variable within your JuiceController class:
public function insertjuice(Request $request)
{
dd($request->all());
}
Problem with displaying content entered in the form in laravel.
What code to add to display the value entered in the form?
--web.php
Route::get('/show-name', ['uses' => 'NameController#show-name', 'middleware' => 'auth']);
--
NameContoller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name()
{
return view('show-name');
}
}
--
show-name.blade.php
<?php
print_r($_POST);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Message after using the button - MethodNotAllowedHttpException.
I will be grateful for your help.
You're sending a POST request, not a GET request.
Route::post('/show-name', [
'uses' => 'NameController#show-name',
'middleware' => 'auth'
]);
The documentation tells you how to get all the data
https://laravel.com/docs/5.8/requests#retrieving-input
The documentation tells you have to pass data to a view https://laravel.com/docs/5.8/views#passing-data-to-views
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name(Request $request)
{
$input = $request->all();
return view('show-name')->with('data', $input);
}
}
The documentation tells you how to access data passed into a view https://laravel.com/docs/5.8/blade#displaying-data
#php
echo print_r($data);
#endphp
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Further more, the form won't work without CSFR https://laravel.com/docs/5.8/blade#forms
Inputs are self closing, meaning there is no need for </input>
Further more, there is no need for <?php echo $_SERVER['PHP_SELF']; ?> because with Laravel, you can specifically define the name of the route.
<form method="post" action="/show-name" class="">
Watch some basic tutorials on Laravel because you're going about this all incorrectly.