(SOLVED) Thanks..
I just want to make a new view called "tambah.blade.php" and the controller is "JurnalController.php" with method "tambahJurnal", but it show an error. What's wrong with my route?
Here is my form:
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
And here is my method in JurnalController:
public function tambahJurnal(Request $request){
$jurnal = new Jurnals;
$jurnal->no_jurnal = $request->no_jurnal;
$jurnal->tgl_jurnal = $request->tgl_jurnal;
$jurnal->keterangan = $request->keterangan;
$jurnal->save();
}
This is my route:
Route::post('/tambah', 'JurnalController#tambahJurnal');
And it show an error like this:
enter image description here
You are creating a route for the POST method with this line:
Route::post('/tambah', 'JurnalController#tambahJurnal');
But then, you're trying to perform a GET request with your browser on that URL. That's why you're getting that error.
Try adding this line as well:
Route::get('/tambah', 'JurnalController#tambahJurnal');
#1. Add this route in your route file.
Route::get('/tambah', function()
{
return view('tambah');
});
#2. change in tambah.blade.php file
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
to
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
Thanks
You can write this. Hopefully this will solve your problem.
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
try in form action ="/tambah"
try with
Route::any('/tambah', 'JurnalController#tambahJurnal');
first then if it works fine you can change to
Route::post('/tambah', 'JurnalController#tambahJurnal');
any will work for get post put ....
Change this line
Route::post('/tambah', 'JurnalController#tambahJurnal');
to
Route::post('tambah', 'JurnalController#tambahJurnal')->name('tambah');
and use blade Form
<h1>Tambah Jurnal</h1>
{!! Form::open(['route' => 'tambah','method' => 'POST','class' => 'tambahJurnal']) !!}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
{!! Form::close() !!}
Advantage of using blade Form is , you don't explicitly need to specify {{ csrf_field() }}, blade injects csrf token itself.
Add route to show view
Route::get('/tambah', 'JurnalController#index');
And add index method to your controller
public function index(){
return view("tambah");
}
Also add /
action="{{ route('/tambah') }}"
Sometimes it happened that you are in /tambah and trying to post url becomes /tambah/tambah
Related
I´m traying to create form to create event.
i have one resourceRoute in my laravel 7. But when i send my form, return this message:
The POST method is not supported for this route
but how i said i have a resource route, and i call to method create.
My routes
Route::resource('calendario', 'CalendarioController');
My form:
div class="col-xs-12 p-5">
<form action="{{ Request::is('calendario/*/edit') ? route('calendario.update', $event->id) : route('calendario.create') }}" method="post">
#csrf
#if(Route::currentRouteName() == 'calendario.edit')
#method('PUT')
#endif
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" name="nombre" id="nombre" value="{{ isset($event) ? $event->nombre : old('nombre') }}" aria-describedby="emailHelp" placeholder="Nombre del cliente">
</div>
<div class="form-group">
<label for="fecha-inicio">Fecha Inicio</label>
<input type="text" value="{{ isset($event) ? $event->fecha_inicio : old('fecha_inicio') }}" class="form-control" name="fecha_inicio" id="fecha-inicio">
</div>
<div class="form-group">
<label for="fecha-inicio">Fecha Fin</label>
<input type="text" value="{{ isset($event) ? $event->fecha_fin : old('fecha_fin') }}" class="form-control" name="fecha_fin" id="fecha-fin">
</div>
<input type="submit" class="btn btn-info" value="{{ Request::is('calendario/*/edit') ? 'Guardar' : 'Crear Cita' }}">
</form>
</div>
</div>
</div>
thsi form it´s used also for edit event and in edit i can ok... I don´t understand that i´m doing wrong
Thanks you for help
This is 100% an issue with the Route::resource('calendario', 'CalendarioController')
The update method accepts put as I can remember.
As you can see here: put/patch
So you can change your form method to method="put" or your have to define your routes like this: Route::post('path/{id}', 'CalendarioController#update')
Hello I want to shift the _token in the last of URL asshown below
What I am getting:
example.com/search?_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q&query=php
What I want:
example.com/search?query=php&_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q
My Form code Is like
<form action="search" method="GET">
{!! csrf_field() !!}
<div class="main-search-input fl-wrap">
<div class="main-search-input-item">
<input type="text" name="query" value="" placeholder="Search snippets..." required>
</div>
<button class="main-search-button" type="submit">Search</button>
</div>
</form>
Put the csrf field at the bottom of the form tho I think you don't need it.
I am trying to submit a form in Laravel but I am getting the error The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I have tried the suggestions in post method in laravel give MethodNotAllowedHttpException but none is working.
Here is my code.
<div class="row" style="background: #ffffff;">
<div class="col-lg-12 col-md-12 col-sm-12" style="background: white; margin: 10px">
<form method="post" action="{{ route('companies.update',[$company->id]) }}">
{{ csrf_field() }}
<input type="hidden" name="method" value="put">
<div class="form-group">
<label for="company.name">Name <span class="required">*</span> </label>
<input placeholder="Enter name" id="company-name" required name="description" spellcheck="false" class="form-control" value="{{ $company->name }}" />
</div>
<div class="form-group">
<label for="company-content">Description</label>
<textarea placeholder="Enter Description" style="resize: vertical" id="company-content" name="description" rows="5" spellcheck="true" class="form-control autosize-target text-left">
{{$company->description}}</textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit" />
</div>
</form>
</div>
</div>
Replacing post with get,put removes the error but not doing what I want.
These are my routes
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('companies','CompaniesController');
Route::resource('projects','ProjectsController');
Route::resource('roles','RolesController');
Route::resource('tasks','TasksController');
Route::resource('users','UsersController');
In the CompaniesController I have
public function update(Request $request, Company $company)
{
$companyupdates = Company::where('id', $company->id)->update([
'name' => $request->input('name'),
'description' => $request->input('description'),
]);
if($companyupdates){
return redirect()->route('companies.show', ['company'=>$company->id])->with('success','Company Updated Successfully');
}
return back()->withInput();
}
Where am I going wrong?
Try using the blade directives instead:
<form method="post" action="{{ route('companies.update',$company->id) }}">
#csrf
#method('PUT')
Note: you don't need to pass the company id with '[ ]'
In this input:
<input type="hidden" name="method" value="put">
The name should be _method according to the laravel form method spoofing
Example from the docs:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
With the blade directives:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>`
Why is this error occurring?
You put the wrong name on your method input, so laravel will recognize this form action as POST, and not PUT. Since it's a update action, laravel will thrown this error.
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
For more info: Docs
I am using Laravel and I am trying to create an edit page and call my update method on submit, the problem is I am getting a 404 when updating. This is my blade file for editing like so:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Here are my routes:
Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController#edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController#update');
And Here are the methods being called:
public function edit($id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
public function update(Request $request, $id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
Why am I getting a 404 error and how do I fix it?
Thanks,
In laravel docs, HTML forms do not support PUT, PATCH or DELETE
actions. So, when defining PUT, PATCH or DELETE routes that are called
from an HTML form, you will need to add a hidden _method field to the
form. The value sent with the _method field will be used as the HTTP
request method:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
You may use the #method Blade directive to generate the _method input:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
There are so many issues in your code lets resolve one by one:
action="/admin/professions-update/{{ $data->pkprofession }}">
change it to:
action="{{ url('/admin/professions-update/' . $data->pkprofession) }}">
and then HTML forms do not support PUT, PATCH or DELETE actions, so chage it to:
<form action="{{ url('/admin/professions-update/' . $data->pkprofession) }}" method="POST">
#method('PUT')
#csrf // this is required when you are using the method other then 'get'
other elements
</form>
You're missing the csrf token and the method input. Try this:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="POST" action="/admin/professions-update/{{ $data->pkprofession }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Also, in your update method you are forgeting to update the object, add this to your code:
$data->update($request->all());
For more info: DOCS
I will process the data from the form
then I click the add button and get an error
Whoops, looks like something went wrong.
TokenMismatchException in VerifyCsrfToken.php line 67:
i have view
<form action="{{ url('siswa') }}" method="post">
<div class="form-group">
<label for="nisn" class="control-label">NISN</label>
<input name="nisn" id="nisn" type="text" class="form-control">
</div>
<div class="form-group">
<label for="nama_siswa" class="control-label">Nama Siswa</label>
<input name="nama_siswa" id="nama_siswa" type="text" class="form-control">
</div>
<div class="form-group">
<label for="tanggal_lahir" class="control-label">Tanggal Lahir</label>
<input name="tanggal_lahir" id="tanggal_lahir" type="date" class="form-control">
</div>
<div class="form-group">
<label for="jenis_kelamin" class="control-label">Jenis Kelamin</label>
<div class="radio">
<label><input name="jenis_kelamin" type="radio" value="L" id="jenis_kelamin"> Laki-laki</label>
</div>
<div class="radio">
<label><input name="jenis_kelamin" type="radio" value="P" id="jenis_kelamin"> Perempuan</label>
</div>
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Tambah Siswa">
</div>
</form>
and then this is my controller
public function create()
{
return view('siswa.create');
}
public function store(Request $request)
{
$siswa = $request -> all();
return $siswa;
}
you need to add {{csrf_field()}} inside the form. it will create a csrf token, which is needed to submit a form
You need to add this {{ csrf_field() }} between your form tags.Read here for more information https://laravel.com/docs/5.4/csrf
There are many options to solve this problem.
1) You can take hidden input field for token inside your form like:
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
2) Add following code before the closing tag of your form:
{{ Form::token() }}
3) Or use laravel form syntax to avoid token mismatch problem like below.
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}
4) Or in the html form structure you can also use csrf field like below.
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
5) Or lastly.
<form method="POST" action="/profile">
{!! csrf_field() !!}
...
</form>
This will definately work for you.
Thanks