MethodNotAllowedHttpException? - laravel

I'm getting this error when i want to delete ?
<form action="{{ URL::route('admin.property.features.delete',$feature-
>id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button class="btn-block btn-link delete-btn admin-delete">Delete</button>
</form>
this is my route:
Route::get('admin/property/features/{id}/delete', ['as' => 'admin.property.features.delete', 'uses' => 'Admin\AdminPropertyFeaturesController#destroy']);
controller:
public function destroy($feature_id){
$feature = Feature::findOrFail($feature_id);
dd($feature);
$feature->delete();
return redirect()->back()->withFlashMessage('Property features has been deleted successfully!!');
}

Simple error. You are only accepting GET request in your route list. And the request you are making from view is POST. So that's why it showing method is not allowed.
Change the route to POST and it will work.
Route::post('admin/property/features/{id}/delete', ['as' => 'admin.property.features.delete', 'uses' => 'Admin\AdminPropertyFeaturesController#destroy']);
Edit:
Use any to accept any kind of the requests.
Route::any('admin/property/features/{id}/delete', ['as' => 'admin.property.features.delete', 'uses' => 'Admin\AdminPropertyFeaturesController#destroy']);

Related

The GET method is not supported for this route. Supported methods: POST in Laravel 7

Can anyone tell me the solution to this?
Below I have mentioned web.php, Domaincontroller.php and HTML form.
Please tell me did I make a mistake in it.
web.php
Route::get('/domains','Domaincontroller#ShowDomains');
Route::post('/add_domain','Domaincontroller#AddNewDomain');
Domaincontroller.php
public function AddNewDomain(Request $request){
$this->validate($request, [
'domain_name' => 'required',
'domain_register_date' => 'required',
'domain_expiry_date' => 'required',
'domain_registrar' => 'required',
'registrar_username' => 'required',
'registrar_password' => 'required',
'registrar_email' => 'required'
]);
$domain = new Domain([
'domain_name' => $request->get('domain_name'),
'domain_register_date' => $request->get('domain_register_date'),
'domain_expiry_date' => $request->get('domain_expiry_date'),
'domain_registrar' =>$request->get('domain_registrar'),
'registrar_username' => $request->get('registrar_username'),
'registrar_password' => $request->get('registrar_password'),
'registrar_email' => $request->get('registrar_email')
]);
$domain->save();
return redirect()->route('domain.add_domain')->with('Domain has been added.');
}
}
HTML Form:
<form method="post" action="{{url('/')}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Your form dont send to the right url
<form method="post" action="{{url('/add_domain')}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
You should define named routes first:
Route::post('/add_domain','Domaincontroller#AddNewDomain')->name('adddomain');
And then reference the route in your form action:
<form method="post" action="{{ route('adddomain') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Just a little further cleaning, you can just use #csrf instead writing the entire input field for the CSRF token:
<form method="post" action="{{ route('adddomain') }}" enctype="multipart/form-data">
#csrf
// Your other fields here
</form>

How to create ONE Post route to allow changes/updates on many pages as admin?

I have created an app and now it should allow an admin to update the content from the fronted. I have integrated TinyMCE. So far so good. However, instead of creating 100's of routes
Route::post('/category1/person1', [
'uses' => 'MainController#infoupdate',
'as' => 'infoupdate',
]);
Route::post('/category1/person2', [
'uses' => 'MainController#infoupdate',
'as' => 'infoupdate2',
]);
etc...
With DRY principle in mind, I want to use only ONE route.
I have tried using the where filter but that is giving me an error:
missing route parameters
Route::post('/{type}/{person}', [
'uses' => 'MainController#infoupdate',
'as' => 'infoupdate',
])->where(['type' => '(actors|authors)', 'person' => '.*']);
The View:
#if($admin)
<form action="{{ route('infoupdate') }}" method="post">
<div class="form-group">
<label for="textareaeditor"></label>
<textarea name="content" rows="10" class="form-control" id="textareaeditor"></textarea>
</div>
{{ csrf_field() }}
<button type="submit">Update</button>
</form>
#endif
What am I missing?
The issue is in your view. When creating the form, you must pass the required parameters for creating the route:
#if($admin)
<form action="{{ route('infoupdate', ['type' => 'actors', 'person' => 'person1']) }}" method="post">
<div class="form-group">
<label for="textareaeditor"></label>
<textarea name="content" rows="10" class="form-control" id="textareaeditor"></textarea>
</div>
{{ csrf_field() }}
<button type="submit">Update</button>
</form>
#endif

Laravel post request not works

I have Form in Laravel and when i submit the form to redirect another page("action = panel") with inputs's value. but problem is that when i enter in another's link it displays error. what is wrong?
This is form
this is another page when submit form
this is error when i enter in link again
this is form code:
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
this is routes:
Route::get('/', [
'uses' => 'AdminController#getAdminIndex',
'as' => 'index.admin'
]);
Route::post('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
this is controller:
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
return view('admin/admin', ['name' => $request->name]);
}
}
this is because when you enter an address in address bar, your are actually sending a get request. but you've defined your route with post method!
to fix this you can use any:
Route::any('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
and in controller:
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
$name = $request->name ?: Auth::user()->name;
return view('admin/admin', ['name' => $name]);
}
}
Try to use the following statement in form as {{ csrf_field() }}
Sometimes routes may create you an issue. Try below snippet.
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Correct your Routes as below and try.
Route::post('/adminPanel', ['uses' => 'AdminController#getAdminPanel', 'as' => 'adminPanel' ]);

Submitting form input after logging in with laravel

I have the view:
<form class="text-center" action="{{route('PostComment')}}" method="POST">
<div class="form-group">
<textarea class="form-control" name="Comment" id="exampleTextarea" placeholder="Write down your thought here..." rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
And also the route:
Route::post('/Comment', [
'uses' => 'CommentController#Comment',
'as' => 'PostComment',
'middleware' => 'auth'
]);
And the controller(not so important):
public function Comment(Request $request)
{
$this->validate($request, [
'Comment' => 'required|min:10|max:100',
]); // validation of comment
$NewComment = new Comment();
$NewComment->user_id = Auth::user()->id;
$NewComment->text = $request['Comment'];
$NewComment->save();
return redirect()->route('Debate');
}
My question is that when you submit the form data and you are not logged in, you have to log in but the form isn't submitted?(edited:using laravel auth)
How to make this thing work, you fill the textarea, you forgot to log in, you log in and the form is submited?

I can't delete my status

My error is:
FatalErrorException in 077cf636f32dba5a90c4b83021f7bfea049823d7.php line 0: Method Illuminate\View\View::__toString() must not throw an exception
My route:
Route::get('/delete-status/{status_id}', [
'uses' => 'Classroom#getDeleteStatus',
'as' => 'Status.delete',
'middleware' => 'auth'
]);
My controller:
public function getDeleteStatus($status_id)
{
$status = Status::where('id', $status_id)->first();
$status->delete();
return redirect()->route('class')->with(['message' => 'Successfully deleted!']);
}
My view:
<div style="text-align: right">
Edit ||
Delete
</div>
What should I do?
I think your route should be delete. Route::delete
Route::delete('/delete-status/{status_id}', [
'uses' => 'Classroom#getDeleteStatus',
'as' => 'Status.delete',
'middleware' => 'auth']);
What I did way back then is a Resource controller, but you can use Route::delete
you can check more about resource controller here: https://laravel.com/docs/5.2/controllers
in your view:
<form action="action('Classroom#getDeleteStatus', {{$status_id}})" method="POST">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-xs btn-danger pull-left"><i class="fa fa-trash"></i></button>
</form>
then in your controller
public function getDeleteStatus($status_id)
{
$status = Status::where('id', $status_id)->first();
$status->delete();
return redirect()->route('class')->with(['message' => 'Successfully deleted!']);
}
yes i fixed my problem...
my view should be
<div style="text-align: right">
Edit ||
Delete

Resources