I'm trying to insert my data to database from form.
My URL to create the data is web.com/siswa/create
But when I click submit system show error MethodNotAllowedHttpException.
How I can fix it? Is there anything wrong with my code?
Here is my form:
<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>
Controller:
public function tambah()
{
return view('siswa.create');
}
public function store(Request $request)
{
$siswa = new \App\Siswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}
Route:
Route::get('/siswa/create', [
'uses' => 'SiswaController#tambah',
'as' => 'tambah_siswa'
]);
Route::get('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
change your store function route from get to post
Route::post('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
Use Csrf protection field in your form for the session timeout error
{{ csrf_field() }}
OR
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
OR if you are using Form builder
{!! Form::token() !!}
In Route please use post instead of get
Route::post('/siswa','SiswaController#store');
and also include {{ csrf_field() }} in form
you are using method="POST" on your form but in on your route you are using Route::get
Use Route::post for your route
In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.
Try this
Route::post('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
You are using POST method in your form and using GET in route.
try this
Route::post( '/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
] );
Related
I am trying to submit form to the laravel. However, I am facing strange issue that the form is being submitted without any data. When I tried to print_r on the cotroller it displays:
Array
(
)
My form looks like this:
<form method="POST" action="{{route('amenityFront.index')}}" id="buildingByProperty">
#csrf
<select class="form-control" name="property" id="propertySelect">
#foreach($properties as $pk => $pv)
<option value="{{$pv->id}}" {{ ($pv->id == $selectedProperty)?'selected':'' }}>{{$pv->property_name}}</option>
#endforeach
</select>
<button type="submit">Submit</button>
</form>
And this is my route. Is it because of that match route? I am using this match route for first time.
Route:
Route::match(['get', 'post'],'/', ['as' => 'amenityFront.index', 'uses' => 'AmenityController#index']);
Update:
I have updated form to :
<form method="POST" action="{{route('amenityFront.postIndex')}}" id="buildingByProperty">
#csrf
<select class="form-control" name="property" id="propertySelect">
#foreach($properties as $pk => $pv)
<option value="{{$pv->id}}" {{ ($pv->id == $selectedProperty)?'selected':'' }}>{{$pv->property_name}}</option>
#endforeach
</select>
<button type="submit">Submit</button>
</form>
and updated route to:
Route::get('/', ['as' => 'amenityFront.index', 'uses' => 'AmenityController#index']);
Route::post('/', ['as' => 'amenityFront.postIndex', 'uses' => 'AmenityController#postIndex']);
However, instead of redirecting to postIndex I am still being posted to #index
My route:list
What am I trying to achieve?
I have "tasks" and each task can have multiple "notes", so when you select a Task and click notes, it takes you to a page with all the notes for the task in which you clicked.
Each note has a field called "task_id", so my problem is passing this task_id to the note.
I'm trying to pass it like this on the notes form:
<form method="POST" action="{{route('notes.store',$task)}}">
#include('notes.form')
</form>
And it goes into my controller
public function store(Request $r)
{
$validatedData = $r->validate([
'note' => 'required',
]);
$r['created_by'] = Auth::user()->user_id;
return $r;
/*
$note = Note::create($r->all());
return redirect('/notes')->with('store');
*/
}
But I return it to see how its going and I get this:
{"_token":"OmGrbYeQDl35oRnmewrVraCT0SHMC16wE4gD56nl","note":"363","created_by":4,"8":null}
That 8 at the end is actually the correct task id, but it appears as the name instead of the value.
What may be causing this?
This is my form view:
#csrf
<div class="col">
<div class="form-group">
<input type="text" class="form-control" name="note">
</div>
</div>
<div class="col-10">
<div class="form-group">
<button class="btn btn-success" type="submit">Add note</button>
<br><br>
</div>
</div>
These are my routes:
Route::get('/tasks/{task}/notes', ['as' => 'tasks.notes', 'uses' => 'NoteController#index']);
Route::get('/projects/{project}/tasks', ['as' => 'projects.tasks', 'uses' => 'ProjectController#seeTasks']);
Route::get('/projects/results','ProjectController#filter');
Route::get('/tasks/results','TaskController#filter');
Route::resource('projects','ProjectController');
Route::resource('clients','ClientController');
Route::resource('tasks','TaskController');
Route::resource('users','UserController');
Route::resource('notes','NoteController');
You are trying to pass the task_id as a route parameter, but your notes.store route has no route parameters.
Verb Path Action Route Name
POST /notes store notes.store
Adding the task_id as a hidden input should properly send it with the request:
<form method="POST" action="{{ route('notes.store') }}">
<input type="hidden" name="task_id" value="{{ $task->id }}">
#include('notes.form')
</form>
I'm trying to create a new "Airborne" test in my program and getting a 405 MethodNotAllowed Exception.
Routes
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController#create'
]);
Controller
public function create(Request $request, $id)
{
$airborne = new Airborne;
$newairborne = $airborne->newAirborne($request, $id);
return redirect('/testing/' . $id . '/airbornes/' . $newairborne)->with(['id' => $id, 'airborneid' => $newairborne]);
}
View
<form class="sisform" role="form" method="POST" href="{{ URL::to('AirborneController#create', $id) }}">
{{ csrf_field() }}
{!! Form::token(); !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
</form>
According to my knowledge forms don't have a href attribute. I think you suppose to write Action but wrote href.
Please specify action attribute in the form that you are trying to submit.
<form method="<POST or GET>" action="<to which URL you want to submit the form>">
in your case its
<form method="POST" ></form>
And action attribute is missing. If action attribute is missing or set to ""(Empty String), the form submits to itself (the same URL).
For example, you have defined the route to display the form as
Route::get('/airbornes/show', [
'uses' => 'AirborneController#show'
'as' => 'airborne.show'
]);
and then you submit a form without action attribute. It will submit the form to same route on which it currently is and it will look for post method with the same route but you dont have a same route with a POST method. so you are getting MethodNotAllowed Exception.
Either define the same route with post method or explicitly specify your action attribute of HTML form tag.
Let's say you have a route defined as following to submit the form to
Route::post('/airbornes/create', [
'uses' => 'AirborneController#create'
'as' => 'airborne.create'
]);
So your form tag should be like
<form method="POST" action="{{ route('airborne.create') }}">
//your HTML here
</form>
MethodNotAllowedHttpException signposts that your route isn't available for the HTTP request method specified. Perhaps either because it isn’t defined correctly, or it has a conflict with another similarly named route.
Named Routes
Consider using named routes to allow for the convenient generation of URLs or redirects. They can generally be much easier to maintain.
Route::post('/airborne/create/testing/{id}', [
'as' => 'airborne.create',
'uses' => 'AirborneController#create'
]);
Laravel Collective
Use Laravel Collective's Form:open tag and remove Form::token()
{!! Form::open(['route' => ['airborne.create', $id], 'method' =>'post']) !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
{!! Form::close() !!}
dd() Helper Function
The dd function dumps the given variables and ends execution of the script. Double-check your Airborne class is returning the object or id you expect.
dd($newairborne)
List available routes
Always make sure your defined routes, views, and actions match up.
php artisan route:list --sort name
First of All
Form don't have href attribute, it has "action"
<form class="sisform" role="form" method="POST" action="{{ URL::to('AirborneController#create', $id) }}">
Secondly
If the above change doesn't work, you can make some changes like:
1. Route
Give your route a name as:
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController#create',
'as' => 'airborne.create', // <---------------
]);
2. View
Give route name with route() method in form action rather than URL::to() method:
<form class="sisform" role="form" method="POST" action="{{ route('airborne.create', $id) }}">
I would like to start by apologizing for the newbish question.
I'm in the process of making a simple CRUD controller on Laravel.
My create method is as follows:
public function create(Request $request)
{
$dummy = new Dummy();
$dummy->title = $request->title;
$dummy->content = $request->dummy_content;
$dummy->created_at = new \DateTime();
$dummy->updated_at = new \DateTime();
$dummy->save();
return redirect()
->route('index/view/', ['id' => $dummy->id])
->with('message', 'Dummy created successfully');
}
my view method:
public function view($id)
{
$dummy = Dummy::find($id);
return view('index/view', [
'dummy' => $dummy
]);
}
my corresponding routes:
Route::get('index/view/{id}', 'IndexController#view');
Route::post('index/create', 'IndexController#create');
and my form:
<form action="create" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control">
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea name="dummy_content" cols="80" rows="5" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-default btn-sm">Submit</button>
</form>
When I submit my form I get the following exception:
InvalidArgumentException in UrlGenerator.php line 314:
Route [index/view/] not defined.
I've been stuck here for quite some time and I still can't figure out why I'm not generating my route properly.
What am I missing?
You are trying to call a route when instead you should call the controller. This will do the trick
return redirect()->action('IndexController#view', ['id' => $id])->with($stuff);
Also, i suggest you to define aliases to routes, so you could do something like
In your controller:
return Redirect::route('route_alias', ['id' => $id])->with($stuff);
In your routes:
Route::get('/index/view/{id}', [
'as' => 'route_alias',
'uses' => 'IndexController#view'
]);
After I deploy my Laravel web application to my host, my code was not work as local. I have a form and POST it to a route, it calls a controller that handles any type of requests. I want to react for each type of request, but Request::Method() function returns GET.
Route:
Route::any('/', [
'as' => 'root', 'uses' => 'WelcomeController#index']);
Blade:
<form action="{{ URL::route('root') }}" method="POST">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}">
<input type="hidden" name="newsId" value="{{ $newsId }}">
<input type="hidden" name="orders" id="orders" value="">
<button class="btn" name="next">NEXT</button>
<button class="btn" name="save">SAVE</button>
</form>
Controller:
if (Request::isMethod('get')) {
$newsId = (Auth::user()->last_news_id % 100) + 1;
$sentences = News::find($newsId)->sentences;
return view('summarizer')->with(['sentences' => $sentences, 'newsId' => $newsId]);
} elseif (Request::isMethod('POST')) {
return 'post';
}
Also I used Request::Method() and it retures GET for all time!
I tested this codes in my localhost and it works perfectly.
EDIT:
I'm surprising that Input::all() returns empty value too. It works in local well.
Try this way -- Make the Request class dependency injection in index() method. like this
public function index(Request $request) {
if ($request->isMethod('get')) {
// your code
}elseif ($request->isMethod('post')) {
// your code
}
The best and the easier way is to create 2 routes,
Route::get('/', [
'as' => 'root', 'uses' => 'WelcomeController#getFunction']);
Route::post('/', [
'as' => 'root', 'uses' => 'WelcomeController#postFunction']);
Then create two function into your controller .
Now try to add dd(Input::all()) into your postFunction to test if The Input:all() is still empty.
On more thing , is better practise to use blade to create your form:
{{ Form::open(['route' => 'root','method' => 'post']) }}
//
{{ Form::close() }}
I had a similar problem. Changing the request from http to https fixed it for me. In my case the server forced requests to https. Hope it helps.