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) }}">
Related
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'
] );
I want to pass an input value from one blade file to another blade file.
I'm new to PHP Laravel, and I'm getting an error when attempting to use it.
I think my syntax is wrong here. Can somebody help?
channeling.blade:
<select class="form-control " name="fee" id ="fee"></select>
This is the link to the next page, where i want to send the value of "fee":
<input type="hidden" value="fee" name="fee" />
Click to Channel</p>
This is my web.php:
Route::post('pay', [
'as' => 'fee',
'uses' => 'channelController#displayForm'
]);
This my controller class:
public function displayForm()
{
$input = Input::get();
$fee = $input['fee'];
return view('pay', ['fee' => $fee]);
}
Error message:
Undefined variable: fee
(View: C:\xampp\htdocs\lara_test\resources\views\pay.blade.php)
pay.blade:
<h4>Your Channeling Fee Rs:"{{$fee}}"</h4>
You should use form to send post request, since a href will send get. So, remove the link and use form. If you use Laravel Collective, you can do this:
{!! Form::open(['url' => 'pay']) !!}
{!! Form::hidden('fee', 'fee') !!}
{!! Form::submit() !!}
{!! Form::close() !!}
You can value inside a controller or a view with request()->fee.
Or you can do this:
public function displayForm(Request $request)
{
return view('pay', ['fee' => $request->fee]);
}
I think you can try this, You mistaken url('pay ') with blank:
change your code:
Click to Channel</p>
to
Click to Channel</p>
Further your question require more correction so I think you need to review it first.
You can review about how to build a form with laravel 5.3. Hope this helps you.
You have to use form to post data and then you have to submit the form on click event
<form id="form" action="{{ url('pay') }}" method="POST" style="display: none;">
{{ csrf_field() }}
<input type="hidden" value="fee" name="fee" />
</form>
On the click event of <a>
<a href="{{ url('/pay') }}" onclick="event.preventDefault();
document.getElementById('form').submit();">
Logout
</a>
tl;dr: I believe #AlexeyMezenin's answer is the best help, so far.
Your current issues:
If you have decided to use Click to Channel, you should use Route::get(...). Use Route::post(...) for requests submitted by Forms.
There isn't an Input instance created. Input::get() needs a Form request to exist. Thus, the $fee an Undefined variable error message.
The value of <input type="hidden" value="fee" name="fee"/> is always going to be the string "fee". (Unless there's some magical spell casted by some JavaScript code).
The laravel docs suggest that you type-hint the Request class when accessing HTTP requests, so that the incoming request is automatically injected into your controller method. Now you can $request->fee. Awesome, right?
The way forward:
The BasicTaskList Laravel 5.2 tutorial kick-started my Laravel journey.
I changed the code like this and it worked..
echanneling.blade
<input type="hidden" value="fee" name="fee" />
<button type="submit" class="btn btn-submit">Submit</button>
channelController.php
public function about(Request $request)
{
$input = Input::get();
$fee = $input['fee'];
return view('pay')->with('fee',$fee);
}
Web.php
Route::post('/pay', 'channelController#about' );
Missing argument 2 for App\Http\Controllers\Company\OrderController::store()
I got this error in my store controller, my form is passing 2 parameters but the second one is not found.
Route: Route::resource('order', 'OrderController');
$company gets converted to a model in the controller.
The form:
<form class="form-horizontal" role="form" method="POST" action="{{action('Company\OrderController#store', [$company,$orderid])}}">
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Any ideas?
Thanks!
If you created store route with Route::resource() it doesn't expect any parameters and should look like this:
public function store(Request $request)
So, you need to pass data using hidden inputs, like:
{!! Form::hidden('data', 'some data') !!}
And then get data in controller with:
$data = $request->data;
You should specify the key-value pair like this:
['company_id' => $company->id, 'order_id' => $order->id]
So your form would look like:
<form action="{{ action('Company\OrderController#store', ['company_id' => $company->id, 'order_id' => $order->id]) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Hope this helps!
I'm trying to delete a single record by id. Instead, it deletes all records in that table.
Here's my code:
View
<form role="form" action="{{ route('status.delete', ['statusId' => $status->id]) }}" method="post">
<button type="submit" class="btn btn-default"><i class="fa fa-times"></i> Delete</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
Routes
Route::post('/status/{statusId}/delete', [
'uses' => '\Dashboard\Http\Controllers\StatusController#deleteStatus',
'as' => 'status.delete',
'middleware' => ['auth'],
]);
Controller
public function deleteStatus(Request $request, $statusId)
{
Auth::user()->statuses()->delete($statusId);
return redirect()->route('home')->with('success', 'Post deleted.');
}
Note: When I dd($statusId) it does provide the right ID for the status I'm deleting. So that part does work.
This is possible in Laravel 5.6 using the destroy method:
From the docs:
However, if you know the primary key of the model, you may delete the
model without retrieving it. To do so, call the destroy method
App\Model::destroy(1);
or to delete an array of ids:
App\Model::destroy([1, 2, 3]);
or by query:
App\Model::where('active', 0)->delete();
Unfortunately, the Eloquent builder does not support passing the id to delete.
Instead, you have to first find to model, then call delete on it:
$request->user()->statuses()->findOrFail($statusId)->delete();
you can delete the model by using another approach like
App\Models\ModelName::find(id)->delete()
but it throws nullPointerException that you have to handle
**Step 1 create route inside web.php**
Route::delete('/answers_delete/{id}', [App\Http\Controllers\AnswerController::class, 'delete'])->name('answers.delete');
**Step 2 Create method in your controller**
use App\Models\Answer; // use in top of this file
public function delete($id)
{
$ans = Answer::find($id);
$ans->delete();
session()->flash('success', 'Answer Deleted Successfully!!!');
return view('admin.anser.index');
}
**Step 3 define your route name inside form action(Note my case view file name index.blade.php and inside admin/answer/index.blade.php)*
<form action="{{ route('answers.delete', $answer->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger" style="display: inline;">Delete</button>
</form>
I am setting up a simple form in laravel:
This is the route file:
Route::get('backoffice/upload', [ 'as' => 'backoffice/upload',
'uses' => 'UploadController#uploadForm']);
Route::post('backoffice/saveimage',[ 'as' => 'backoffice/saveimage',
'uses' => 'UploadController#saveImage']);
This is the controller:
class UploadController extends \BaseController
{
public function uploadForm()
{
return View::make("backoffice.upload.create");
}
public function saveImage()
{
return "Uploading...";
}
}
And this is the View file:
<h1>Upload Image</h1>
{{ Form::open(['action' => 'UploadController#saveImage']) }}
<div class='formfield'>
{{ Form::label('newfilename','New File Name (optional):') }}
{{ Form::input('text','newfilename') }}
{{ $errors->first('newfilename') }}
</div>
<div class='formfield'>
{{ Form::submit($action,['class'=>'button']) }}
{{ Form::btnLink('Cancel',URL::previous(),['class'=>'button']) }}
</div>
{{ Form::close() }}
// Generated HTML
<h1>Upload Image</h1>
<form method="POST" action="http://my.local/backoffice/saveimage" accept-charset="UTF-8"><input name="_token" type="hidden" value="x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM">
<div class='formfield'>
<label for="newfilename">New File Name (optional):</label>
<input name="newfilename" type="text" id="newfilename">
</div>
<div class='formfield'>
<input class="button" type="submit" value="Create">
</div>
</form>
So, if I go to: http://my.local/backoffice/upload I get the form with the HTML above.
However, if I type anything, then click SUBMIT, I return to the form but now have the following URL:
http://my.local/backoffice/upload?pz_session=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&_token=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&newfilename=ddd
This makes no sense to me. Up until now I have always used route::resource when dealing with forms, and had no problem. I am trying to do a simple form with GET and POST and am having no end of grief. What am I missing?
Furthermore, if I modify routes.php and change it from post to any, then open a browser window and type: http://my.local/backoffice/saveimage then I get the message "Uploading..." so that part is working ok.
Found the solution. In making the backoffice of the system, I had re-used the frontoffice template but removed all the excess. Or so I had thought. However, the front office header template had a form which I had only partially deleted.
So the problem was that there was an opening FORM tag I didn't know about. Consequently, when I clicked on submit to my form, it was actually submitting to this other form.
As the other form had no action it was default to itself.
Of course, had I just validated the HTML this would have shown up straight away. The lesson learned here is to validate my html before submitting questions!
Try this, and be sure to correctly configure your url at app/config/app.php
{{Form::open(['url'=>'backoffice/saveimage'])}}
//code
{{Form::close()}}