Laravel Route resource MethodNotAllowedHttpException on destroy method - php

I am using a resource Laravel route defined by the following line in my routes.php :
Route::resource('test', 'App\Controllers\Teacher\TestController', ['only' => ['index', 'create', 'destroy']]);
The index method works fine. In the template of index I have created a form in order to remove an item of the list.
<form method="DELETE" action="{{ URL::action('App\Controllers\Teacher\TestController#destroy', $audit->id ) }}">
<input type="submit" value="Remove" />
</form>
The URL is correctly generated by Laravel but when I post this form I get the following error :
exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in /var/www/project/bootstrap/compiled.php:5365
I have already try to change DELETE by POST in the method attribute of my form but it doesn't work.
I also read this post but it doesn't help me : MethodNotAllowedHttpException on resource defined method Laravel-4

When you manually create your form you should use POST as method, and use _method input with delete value this way:
<form method="POST" action="{{ URL::action('App\Controllers\Teacher\TestController#destroy', $audit->id ) }}">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="Remove" />
</form>
Reference in Laravel documentation for form method spoofing

Try this:
<form action="/test" ....>

Related

Laravel route post vs get

project is laravel 5.6. My project has 2 routes:
web.php
Route::get('testa', 'HomeController#showTestForm')->name('test');
Route::post('testa', 'HomeController#doTest');
HomeController :
public function showTestForm()
{
Log::warning('from showTestForm');
return view('test');
}
.public function doTest(Request $request)
{
Log::info('from doTest');
// return Input::all();
return view('test', [
'input' => implode(', ', Input::all()),
]);
}
test.blade.php
<form method="post" action="{{ route('test') }}">
#csrf
<input type="text" name="inputvalue">
<button type="submit" class="btn btn-primary">
merge
</button>
</form>
<div>Result</div>
#if(isset($input))
{{$input}}
#endif
Why is working post on route('test') ?
Thank you.
The reason that route('test') works even though your form is a POST request is because route() is just a helper function to generate a url and your GET and POST routes both use the same url.
You've specified in your form to make a post request and it's going to send it to the url provided (which will be the same as your GET request in this case).
Edit:
Route::post('testa', 'HomeController#doTest')->name('postTest');
and then use
<form method="post" action="{{ route('postTest') }}">
Hope this will work
It is because you'are calling the wrong route.
Change :
<form method="post" action="{{ route('test') }}">
to :
<form method="post" action="{{ url('/testa') }}">
or follow the previous answer steps (name the post route then call it)
I believe from the OP they are not understanding how the same route can accept both a GET request and a POST request.
The difference is in how the server receives the data.
Have a read: HTTP Requests

Laravel set route issue for post method

This is my form:
<form class="form-horizontal" method="POST" action="{{ url('/categories/new') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="btn btn-default" value="Cancel" type="reset">
</form>
This is my url where my form is located: /categories/new
This is my route:
Route::get('/categories/new', 'Admin\CategoriesController#newCategory');
I want to keep the new method, so i want to check if there is a post method do smth else load the view with my form. How can I achieve this in laravel 5. I'm a newbie so all of the detailed explanations are welcomed. Thank you !
If you want to use single method for both POST and GET requests, you can use match or any, for example:
Route::match(['get', 'post'], '/', 'someController#someMethod');
To detect what request is used:
$method = $request->method();
if ($request->isMethod('post')) {
https://laravel.com/docs/master/requests#request-path-and-method
Add this to your rotes file:
Route::post('/categories/new', 'Admin\CategoriesController#someOtherFunctionHere');

Php laravel 5.3 passing an input value from one blade file to another blade file

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' );

How to get data transfer from the form into database Laravel 5

I'm trying to transfer data from the form to overwrite the content database .But get the error.
It is my route,method and form.
Route::get('edit/{id}','JokeController#edit');
public function edit(JokeModerateRequest $request,$id) {
$joke = Joke::findOrFail($id);
return view('jokes.edit', [ 'joke' => $joke ]);
}
<form action="/update" method="post">
<input type="text" value="{{ $joke -> content}}" name="body">
<input type="submit" value="Save">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
But when I try use next route and method
Route::post('update/{id}','JokeController#update');
function update(JokeModerateRequest $request,$id)
$joke = Joke::findOrFail($id);
$joke->content = $request -> get('content');
$joke->save();
return back();
I have next error
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 145:
The problem lies here:
<form action="/update" method="post">
This will redirect to /update route (which you haven't defined) instead of /update/id. Those two are different routes.
To fix this, change action of the form to:
<form action="/update/{{ $joke->id }}" method="post">

PasswordReminder in Laravel

I am trying to set a Password reminder in restful way. (Following this tutorial http://laravel.com/docs/4.2/security#password-reminders-and-reset but trying to do it in restful way)
The route looks like this,
Route::group(array('prefix' => 'api/v1'), function(){
Route::resource(
'password', 'RemindersController',
array(
'only' => array('store', 'show', 'update')
)
);
});
RemindersController starts as,
public function update()
{
}
The password reset url is
http://192.x.x.x:8000/api/v1/password/3adb8b0454144ef5aeaa333faa5c575bd833e03d
From this url loading reset.blade as follows,
<form action="{{ action('RemindersController#update') }}" method="PUT"
...
<input type="submit" value="Reset Password"> </form>
But when loading this page, the form action seems to have some issues, the action url does not seem to be right.
<form action="http://192.x.x.x:8000/api/v1/password/%7Bpassword%7D" method="PUT">
What is the right way to provide action property in the form for this? How can I pass the password reset details to 'update' method in Reminder controller?
In the mentioned tutuorial it is like
action="{{ action('RemindersController#postReset') }}" method="POST"
What will change when using the restful resource way?
Got it right by following the suggestion from this site with the following modification,
<form action="{{ URL::to('api/v1/password/update') }}" method="POST">
<input name="_method" type="hidden" value="PUT">

Categories