Laravel 5.2 TokenMismatchException in VerifyCsrfToken.php line 67 - php

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

Related

Laravel 5.7 MethodNotAllowedHttpException

I am getting an MethodNotAllowedHttpException error while im trying to update mine post. So I googled the error and found this laravel throwing MethodNotAllowedHttpException but it get explained that i need to make the route
an post request, where mine form actions go thruw but its already a post and it keeps throwing the same error and i cant figure out if the erros is in the form the web.php or the controller it self
edit.blade.php
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
#method('PUT')
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="body">Body:</label>
<textarea id="body" name="body" class="form-control" rows="10">
{{
$post->body
}}
</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
#include('layouts.errors')
</form>
Web.php
Route::get('/', 'PostsController#index')->name('home');
Route::get('/posts/create', 'PostsController#create');
Route::post('/posts', 'PostsController#store');
Route::get('/posts/{post}', 'PostsController#show');
Route::get('/posts/{post}/edit', 'PostsController#edit');
Route::post('/posts/{post}/edit', 'PostsController#update');
Route::get('/posts/{post}/delete', 'PostsController#destroy');
PostsController.php
(this is the part that matters out of the controller if u want me to post the hole controller let me know)
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'body' => 'required'
]);
$post->update($request->all());
return redirect('/')->with('succes','Product updated succesfully');
}
You should try this:
View file:
<form method="POST" action="{{ route('post.update',[$post->id]) }}">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="body">Body:</label>
<textarea id="body" name="body" class="form-control" rows="10">
{{
$post->body
}}
</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
#include('layouts.errors')
</form>
Your Route
Route::post('/posts/{post}/edit', 'PostsController#update')->name('post.update');
You are submitting the form with the put method as defining #method('PUT') will make it a put route. Either define a route for put method like this Route::put('/posts/{post}/edit', 'PostsController#update'); or remove #method('PUT') from your blade file.
This problem 1 week took me but i solve it with routing
In edit.blade.php
{!! Form::open([route('post.update',[$post->id]),'method'=>'put']) !!}
<div class="form-group">
<label for="title">Title:</label>
{!! Form::text('title',$post->title,['class'=>'form-control',
'id'=>'title']) !!}
</div>
<div class="form-group">
<label for="body">Body:</label>
{!! Form::textarea('body', $post->body, ['id' => 'body', 'rows' => 10,
class => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Edit',['class'=>'btn btn-block bg-primary',
'name'=>'update-post']) !!}
</div>
{!! Form::close() !!}
#include('layouts.errors')
In Web.php
Route::resource('posts','PostsController');
Route::get('/posts/{post}/delete', 'PostsController#destroy');
Route::put('/posts/{post}/edit',['as'=>'post.update',
'uses'=>'PostController#update']);
Route::get('/', 'PostsController#index')->name('home');
Laravel will throw a MethodNotAllowedHttpException exception also if a form is placed by mistake this way inside a table:
<table><form><tr><td>...</td></tr></form><table>
instead of this :
<table><tr><td><form>...</form></td></tr></table>

Search form in Laravel

I have a form with two input fields, that should route to search.servicecity with the two input fields as parameter (service and city). How can I achieve to say, that by clicking the submit button, the input fields are used as the parameters?
{!! Form::open(['route' => ['search.servicecity',service,city]]) !!}
<div class="row">
<div class='col-md-5'>
<input type="text" class='form-control form-control-lg' name="service" id="service" placeholder="activity" data-action="{{ route('search.autocompleteservice') }}"/>
<div id='searchresultservice' style='text-align:left'></div>
</div>
<div class='col-md-5'>
<input type="text" class='form-control form-control-lg' name="city" id="city" placeholder="city or zip" data-action="{{ route('search.autocompletecity') }}"/>
<div id='searchresultcity' style='text-align:left'></div>
</div>
{{ Form::submit('Suchen', array('class'=>'btn btn-success btn-lg btn-block col-md-2'))}}
</div>
{!! Form::close() !!}
You should have code similar to below. You would obviously need to validate your inputs before trusting anything from the client. You could easily add validation in the view also. The routes file could be routes.php depending on your version of laravel.
File: routes\web.php
Route::get('search/servicecity', 'SearchController#index')->name('searchServiceCityForm');
Route::post('search/servicecity', 'SearchController#process')->name('processServiceCity');
File: app\Http\Controller\SearchController.php
class SearchController extends Controller
{
public function index()
{
return view('search.ServiceCity');
}
public function process(Request $request)
{
$service = $request->input('service');
$city = $request->input('city');
/* Do something with data */
return view(search.result, compact('service','city'));
}
File: resources\views\search\ServiceCity.blade.php
<html><head><title>Search for City and Service</title></head><body>
<form method="post" action="{{url('search/servicecity')}}">
{{csrf_field()}}
<div>
<label for="Service">Service:</label>
<input type="text" name="service">
</div>
<div>
<label for="city">City:</label>
<input type="text" name="city">
</div>
</body></html>
File: resources\views\search\Result.blade.php
<html><head><title>Result of Service City Search</title></head><body>
<div><span>Searched for service: {{ $service }}</span></div>
<div><span>Searched for city: {{ $city }}</span></div>
</body></html>
</html>

Laravel model form binding not working for edit

I have a controller TourCategoryController.php and has edit method:
public function edit(TCategory $tCategory)
{
return view('admin.manage.tour.category.edit')->withCategory($tCategory);
}
And below is the code from my view edit:
<div class="col-sm-4">
{{Form::model($category,['route' => ['tour-category.update', $category->id ], 'method' => "PUT"]) }}
<input type="text" class="form-control" id="name" name="name">
<label for="name">Name</label>
{{ Form::close() }}
</div>
The trouble I'm having is, the input field is not being filled with form modal binding.
While inspecting the edit form action attribute shows action="http://localhost:8000/manage/tour-category" while it should be like action="http://localhost:8000/manage/tour-category/{id}"
Route for the controller:
Route::prefix('manage')
->middleware('role:superadministrator|administrator|user')
->group(function () {
Route::resource('tour-category','TourCategoryController');
});
Use laravel text field instead of plain form text field.
{{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
If you are not using Form Facades
<div class="col-sm-4">
<form method="POST" action="{{ route('tour-category.update', $category->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<label for="name">Name</label>
<input type="text" id="name" name="name" class="form-control">
</form>
</div>
use
{{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
instead of
<input type="text" class="form-control" id="name" name="name">

laravel 5.3 MethodNotAllowedHttpException in RouteCollection.php line 218:

(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

Laravel form not submitting data to database

For some reason, my form isn't submitting any data to the database, but it looks fine from where I'm standing and the database can call the information to the form fine.
Since there's a lot of information for people to submit, I'm making the profile details not part of the login process. Still unfamiliar with how Laravel does these but I roughly get the process now that I've been fiddling.
One thing I'm wondering, is there a specific syntax for forms to write to the database, should I be naming the database's respective table names in the form? Or is that part of the Controller?
Should I be using Form Model Binding? It's a little hard to find information on that that's for the current version of Laravel though.
What am I missing?
//Route::get('NewUser', 'UserEntryController#create');
Route::post('NewUser', 'UserEntryController#UserForm');
//Route::get('NewUser', 'UserEntryController#create')->name('NewUser');
Route::post('NewUser', 'UserEntryController#UserForm')->name('submit');
Controller:
<?php
namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
class UserEntryController extends Controller
{
protected function create()
{
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
//return $array;
}
public function UserForm(Request $request) {
$email = $request['email'];
$first_name = $request['first_name'];
$password_hint = $request['password_hint'];
$last_name = $request['last_name'];
$user = UserEdit::find(715)->first();
$user->email = $email;
$user->First_Name = $first_name;
$user->Last_Name = $last_name;
$user->Password_Hint = $password_hint;
$user->save();
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
}
}
Blade:
#extends('layout')
#section('content')
<h1> Add Your Information {{ $id['name'] }}</h1>
<div class="row">
<div class="col-md-6">
<h3>Edit</h3>
<form action="{{ route('submit') }}" method="post">
<div class="form-group">
{{ csrf_field() }}
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="first_name">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="last_name">Your Last Name</label>
<input class="form-control" type="text" name="last_name" id="last_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="password_hint">Your Password Hint</label>
<input class="form-control" type="text" name="password_hint" id="password_hint">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
#foreach ($id as $key=>$value)
{{ $value }}<br>
#endforeach
#stop
You should use $request->get('email') instead of $request['email'] and the same for everything else you want from the request, and I don't think you have to use ->first() when using ->find
There could be something wrong with the _token field, since there are five csrf fields in the form.
Try to remove this line
<input type="hidden" name="_token" value="{{ Session::token() }}">
and just leave one {{ csrf_field() }} in the form.

Categories