Laravel: TokenMismatchException while send form to database - php

When i try to save data from form in laravel allways get this error:
TokenMismatchException in VerifyCsrfToken.php line 68:
But error shows when i access laravel by apache, when i run laravel server by command php artisan serve --host 0.0.0.0 it works perfect...
This is my form view:
<form class="" method="POST" action="{{ $card->path() }}/notes">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<textarea name="body" class="form-control" rows="8" cols="40"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="button">Dodaj</button>
</div>
</form>
This is my controller:
<?php
namespace App\Http\Controllers;
use App\Card;
use App\Note;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store(Request $request, Card $card)
{
$card->notes()->save(
new Note(['body' => $request->body])
);
return back();
}
}
And here is my function that will redirect user after success send form to database:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
//
public function notes()
{
return $this->hasMany(Note::class);
}
public function path()
{
return '/cards/' . $this->id;
}
}

Add this line in header
<meta name="csrf-token" content="{{ csrf_token() }}" />

add this after form tag {{ csrf_field() }}

In Laravel best practice is using Blade for form. It will create token itself.
{!! Form::open(['action'=>$card->path().'/notes','method'=>'post']) !!}
// fields and buttons
{!! Form::close() !!}

You first clear your view by
php artisan view:clear
And then write your view as this
<form class="" method="POST" action="{{ $card->path() }}/notes">
{!! csrf_field() !!}
<div class="form-group">
<textarea name="body" class="form-control" rows="8" cols="40"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="button">Dodaj</button>
</div>
</form>
This might solve your problem

I have the same problem, and fixed it by clear the view cache:php artisan view:clear and then change the storage folder to 777 not 775.

Related

The GET method is not supported for this route. Supported: POST

My form:
#if(!empty($Product) && !empty($ProductSpec))
<form action="{{route('update_product')}}" method="POST">
<div class="form-group">
<label for="product_code">Product Code</label>
<input class="form-control" name="product_code" id="product_code" type="text" placeholder="product code..." value="{{$Product->product_code}}">
</div>
#error('product_code')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
<div class="form-group">
<button type="submit" class="btn btn-primary">Update information</button>
</div>
{{ csrf_field() }}
</form>
#endif
Routes:
Route::resource('add_product','ProductController');
Route::get('edit_product', 'ProductController#find_product_index')->name("edit_product");
Route::post('find_product', 'ProductController#find_product')->name("find_product");
Route::post('update_product', 'ProductController#update_product')->name("update_product");
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Controller function:
public function update_product(Request $request)
{
$this->validate($request, [
'name' => 'required|max:120|string']);
return redirect()->route('edit_product')->with('success', 'Product Updated successfully');
}
I'm using this code and getting the "Get method is not supported for this route, Supported:POST" error.
Got no idea what is wrong with my code? At first I thought it's my MySQL statements from the controller, but I removed them and I'm still having the same problem.
EDIT:
php artisan route:list shows ->
image
Your route should be:
Route::put('update_product/{update_product} ', 'ProductController#update_product')->name("update_product");
Your form should be like:
...............
{{ csrf_field() }}
#method('PUT')
</form>
Run php artisan optimize to clear and reconfig your cache and routes.
Clearing your routes is not enough.

Laravel - Edit and Update Page

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

how to pass value from input form into another page in laravel

Excuse me, i'm new in learning about Laravel and I have a problem about show data value from form input.
I have create.blade.php :
#extends('layouts.master')
#section('content')
<div class="container">
<div class="header">
<h1><b>Create an account</b></h1>
<h5>Welcome to Konoha Village</h5>
</div>
{{ csrf_field() }}
#if(isset($name))
<div class="alert alert-warning alert-dismissible" role="alert">
Halo <strong>{{$name}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">$times;</span>
</button>
</div>
#endif
<div class="form">
<form action="{{ url('final-test') }}" method="post" id="form1">
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
</div>
</div>
#endsection
and my controller with name AccController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AccController extends Controller
{
public function create() {
return view('home.create');
}
public function show(Request $r) {
$soul = $r ->name;
$pesan = "Your name is {$r->name}";
}
}
and my route in web.php :
//route to get play form
Route::get ('start', 'AccController#create' )->name('home.create');
Route::post('final-test', 'AccController#show');
i want to show in another page view that i called show.blade.php :
#extends('layouts.master')
#section('content')
<div>
{{$name = Input::get('name')}}
<h1>Your name is {{ $pesan }}</h1> </div>
#endsection
nothing's error in the end but it couldn't show the value from the input form, would you help me please?
Regards, Aga.
Your web.php:
//route to get play form
Route::get('/start', 'AccController#create')->name('home.create');
Route::post('/final-test', 'AccController#show')->name('home.show');
Added a name home.show to the route final-test.
Your AccController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AccController extends Controller
{
public function create()
{
return view('home.create');
}
public function show(Request $r)
{
$soul = $r->name;
return view('home.show')->with('soul', $soul);
}
}
Your show.blade.php:
#extends('layouts.master')
#section('content')
<div>
<h1>Your name is {{ $soul }}</h1>
</div>
#endsection
Your create.blade.php:
#extends('layouts.master')
#section('content')
<div class="container">
<div class="header">
<h1><b>Create an account</b></h1>
<h5>Welcome to Konoha Village</h5>
</div>
#if(isset($name))
<div class="alert alert-warning alert-dismissible" role="alert">
Halo <strong>{{$name}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">$times;</span>
</button>
</div>
#endif
<div class="form">
<form action="{{ route('home.show') }}" method="post" id="form1">
{{ csrf_field() }}
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
</div>
</div>
#endsection
Here's where the most important change has been made: I moved {{ csrf_field() }} inside the form, so you don't get The page has expired due to inactivity. Please refresh and try again. Also changed the form action to the named route {{ route('home.show') }}.
I kept your <span aria-hidden="true">$times;</span> but this will only show $times;, might need to tweak that.
You are not following conventions here. show method is to data from database. In order to show form data you have do it in store method
HTML Code
There should be some minor changes in html form
{{--Changing in just action--}}
{{-- If it doesn't accept that action then replace it with {{AccController.php#store}} --}}
<form action="AccController.php#store" method="post" id="form1">
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
Then in Controller you will get that form data in store function
public function store(Request $r) {
$soul = $r ->name;
$pesan = "Your name is {$r->name}";
return $pesan;
}
How to get it in route?
Well you are doing some mistakes in route.php. You just have to follow conventions which laravel provides us.
Replace your route code with this.
Route::resource('/posts', 'AccController.php');
Here it will automatically call all by default functions of Controller and assign them particular routes. For example Below the picture
You just have to type php artisan route:list in terminal or command prompt and you will see list of routes which you have created and which laravel creates for you along with method, URI and name. You just have to follow conventions and it will give you results automatically
Give it a try and tell me

Laravel 5.5 - $request->file returns null

I have created a form that uploads a file but it returns a null value when I submit. When I add in the enctype="multipart/form-data" it reloads the page and doesn't seem to go through my controller.
MY HTML FORM
<form class="form-horizontal" role="form" name="importform" method="POST" action="{{ route('import_type') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="control-group">
<label class="control-label"> </label>
<div class="controls">
<div class="control-group text-center">
<label class="btn btn-primary" for="file-selector">
<input id="file-selector" name="template_upload" type="file" value="" required autofocus style="display:none" onchange="$('#upload-file-info').html(this.files[0].name)" required> Upload List </label>
<span class='label label-default' id="upload-file-info"></span>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" id="import-submit" name="import-submit">
</div>
</div>
</form>
MY CONTROLLER: I am using the import method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImportTypeRequest;
use \App\Guest;
use \App\Role;
use \App\User;
use \App\Type;
use Illuminate\Support\Facades\Auth;
class GuestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::with('roles')->where('id', Auth::id())->get();
$types = Type::where('user_id', Auth::id())
->where('active',1)->get();
return view('view_import',compact('user','types'));
}
public function import(ImportTypeRequest $request)
{
$template_upload = $request->file('template_upload');
dd($template_upload);
}
}
Here are some suggested ways trying to solve this.
First of all in your import method add dd($request->all()) at its top and see what's the response. You should see all your form data and of course template_upload file. That's how you make sure that you see all the coming data from your form to your controller method.
Then try to get rid of ImportTypeRequest and just use Illuminate\Http\Request to see what will you get. If you got different result then the problem is in ImportTypeRequest class.
Then why don't you just use $request->template_upload?! It's cleaner I guess.

laravel post does not get picked up?

I am trying to pass a form through. I am using request method to get variables. here is my blade of a form:
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
Routes involved:
Route::get('/admin/gallery', 'GalleryController#manageGallery')->name('manageGallery');
Route::post('/admin/gallery', 'GalleryController#postPhoto')->name('postPhoto');
And this is my controller for it:
class GalleryController extends Controller
{
public function manageGallery() {
return view('home.manageGallery');
}
public function postPhoto(Request $request) {
die("works");
}
}
It does not throw error at me. It just does nothing. So my question is: am I using this method wrong or do I need something more? Thanks in advance.
Firstly make sure that the form you are using is using the correct method for your route
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}" method="post">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
In your controller, put the following in the postPhoto function
public function postPhoto(Request $request)
{
dd($request);
}
You should now get a Request object output to the screen when you submit the form
You may wanna use Blade Forms in order to make Forms in a more natural way for Laravel
{{ Form::open(['route' => '/admin/gallery', 'method' => 'post', 'files' => true]) }}
{{ Form::text('title') }}
{{ Form::label('title', 'Name :') }}
{{ Form::file('file') }}
{{ Form::label('file', 'File :') }}
{{ Form::submit('Add') }}
{{ Form::close() }}
It reduces the overhead of adding the token by yourself as it is automatically added when using the Form facade.
And then, in your controller, you would do something like that to debug when sending the form :
<?php
use Request; /* do not forget this line */
class GalleryController extends Controller
{
public function postPhoto(Request $request)
{
dd($request->toArray());
}
}

Categories