Solution for redirect to POST route Laravel - laravel

SCENARIO:
When I tried to search (POST route) a keyword, results of item will be display as lists. These items has a button (Save or UnSave) on each list. The save button, is either form POST or DELETE method and what happens is when this button clicked, either the item will SAVE or UNSAVE from the tables.
ERROR
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
ROUTES:
Route::post('/jobseeker/jobs/search-results', [JobSeekerController::class, 'searchJobsNow'])->name('jobseeker.jobs.searchNow');
Route::post('/jobseeker/saved-jobs/{id}/save', [JobSeekerController::class, 'saveJob'])->name('jobseeker.savedJobs.save');
Route::delete('/jobseeker/saved-jobs/{id}/unsave', [JobSeekerController::class, 'unsaveJob'])->name('jobseeker.savedJobs.unsave');
CONTROLLER:
public function searchJobsNow(Request $request)
{
// codes
}
public function saveJob()
{
// code for saving a job
return redirect()->back();
}
public function unsaveJob()
{
// code for unsaving a job
return redirect()->back();
}
VIEW
#if ($job->candidateHadSavedTheJob)
<form action="{{ route('jobseeker.savedJobs.unsave', $job->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-primary">Saved</button>
</form>
#else
<form action="{{ route('jobseeker.savedJobs.save', $job->id) }}" method="POST">
#csrf
<button type="submit" class="btn btn-outline-primary"> Save</button>
</form>
#endif

Related

Error message "The GET method is not supported for this route. Supported methods: POST." in laravel 8

Following is my controller. Method ProjectsView is to view all the listings. The second method BackendProjectSearch is for searching of projects. The first page of search result is displayed properly, but when we click on next page it gives the error "The GET method is not supported for this route. Supported methods: POST."
What should I do ?
public function ProjectsView(){
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
public function BackendProjectSearch(Request $request){
$request->validate(["search" => "required"]);
$item = $request->search;
$projects = Projects::where('project_name','LIKE',"%$item%")->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
Following are the routes for both the methods :
Route::post('/backend/project/search', [ProjectsController::class, 'BackendProjectSearch'])->name('backend.project.search');
Route::get('/view', [ProjectsController::class, 'projectsView'])->name('projects.view');
View code :
<div class="col-md-8">
<div class="header-navsearch">
<form class="form-inline mr-auto" method="post" action="{{route('backend.project.search')}}">
#csrf
<div class="nav-search">
<input type="search" name="search" class="form-control header-search" placeholder="Search projects…" aria-label="Search">
<button class="btn btn-primary" type="submit"><i class="fa fa-search"></i></button>
</div>
</form>
</div>
</div>
The route for this request should be post, not get.
Example
Route::post(-----);
Also, make sure to insert a CSRF token while making the request
it means you now cannot do something like the following.
Instead, you have to do something like...
<form action="{{ route('example') }}" method="POST">
#csrf {{-- According to the version of laravel --}}
{{-- Your other codes --}}
<button type="submit" class="">Submit</button>
</form>
You have to use the below code
return redirect()->route('projects.view')->with(['projects' => $projects]);
Your route might be little change
Route::get('/view/{projects?}', [ProjectsController::class, 'projectsView'])->name('projects.view');
and In your controller, you have to change function like this
public function ProjectsView($projects = null){
if($projects!=null){
return view('backend.projects.projects_view',compact('projects'));
}
else{
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects'));
}

Delete function not working with controller

Hello guy's my I just wrote an delete for my project, it lookes like that:
public function projectdelete(Project $project)
{
$project->delete();
return back();
}
My form :
<form action="{{route('project.delete',$project )}}"
method="POST"
onsubmit="return confirm('Are you sure you want to delete the project?')">
#csrf
#method('DELETE')
<button type="submit">Submit</button>
</form>
Route:
Route::delete('/dashboard/project/create/{id}', [
DashboardController::class, 'projectdelete'
])->name('project.delete');
Why is not deleting it?
The problem is on the parameter, your route parameter is {id}, on controller you are using Model Binding, for model binding your route parameter name need to be matched with your model {project} :
Route::delete('/dashboard/project/create/{project}', [
DashboardController::class, 'projectdelete'
])->name('project.delete');
In your blade file pass the project parameter as project and also in your routes give your parameter name as project:
My form :
<form action="{{route('project.delete', ['project' => $project])}}" method="POST" onsubmit="return confirm('Are you sure you want to delete the project?')">
#csrf
#method('DELETE')
<button type="submit">Submit</button>
</form>
Route:
Route::delete('/dashboard/project/create/{project}', [DashboardController::class, 'projectdelete'])->name('project.delete');

laravel form is not submitted

I am new to laravel. This is my view (bids.new):
<form method="post" action="{{ route('bids.storeBid', $project->id) }}" enctype="multipart/form-data" >
#csrf
*content*
<div class="row pt-4">
<button class="btn btn-primary">Place Bid</button>
</div>
</form>
This is my route:
Route::get('/bids/new/{id}', 'BidController#create')->name('bids.new');
Route::post('/bids/{id}', 'BidController#storeBid')->name('bids.storeBid');
BidController:
public function create($id)
{
$project = Projects::findOrFail($id);
return view('bids.new',compact('project'));
}
public function storeBid(Request $request, $id)
{
*content*
}
When I clicked on Place Bid button in my view, the page is not responding and URL is still showing /bids/new/1 which means the storeBid route was not loaded. I tried using dd($id) at controller but it is not displaying as well so I assumed I have a problem with the route or form in the view.
<div class="row pt-4">
<button type="submit" class="btn btn-primary">Place Bid</button>
</div>
try this one. I hope it will help you. Let me know.
Thank you guys for the comments, I figured out the problem was not because of the form or route but due to some errors on my eloquent in the controllers.

Cannot get id of subscription in request

I'm trying to get the id of the selected subscription and pass it to the controller, but for some reason when I dd() or print_r() the id in the controller, it returns the same id regardless of which subscription I click.
Regardless if i click the first '+' button or the second one it always returns the same id when I print_r() it.
Array ( [_token] => iFimNDCv0q4rq7OmmUWGN8SGr2Bq0brsiRMLIzBD [subscription] => 2 )
Even when I click the first one, which should have '1' as the id, it still returns '2'.
This is my route for it in web.php
Route::post('/transaction/store', 'TransactionController#store')->name('transaction.store');
This is the function in the controller
public function store(Request $request)
{
// continue here
print_r($request->input());
}
And this is the form tag that's responsible for send the id (which is inside a foreach statement in blade)
<form id="batch" action="{{route('transaction.store')}}" method="POST">
#csrf
<input type="hidden" name="subscription" value="{{$subscription->id}}" form="batch">
<button type="submit" class="btn btn-sm btn-outline-dark" form="batch">+</button>
</form>
Thanks very much for any help!
I just had to make the form id dynamic!
<form id="batch-{{$subscription->id}}" action="{{route('transaction.store')}}" method="POST">
#csrf
<input type="hidden" name="subscription" value="{{$subscription->id}}" form="batch-{{$subscription->id}}">
<button type="submit" class="btn btn-sm btn-outline-dark" form="batch-{{$subscription->id}}">+</button>
</form>

Laravel 2 submit buttons in the same form

I am building a CRUD with Laravel. Each category hasMany attachments and each attachment belongsTo a category.
In the category.edit view I want to give the user the possibility of deleting the attachments (singularly) from the Category. I tried this method but it did not work:
Registering route for the attachment:
Route::group(['middleware' => ['auth']], function () {
Route::delete('attachment/{id}', 'AttachmentController#delete')->name('attachment');
});
Handling the delete building the AttachmentController#delete method:
class AttachmentController extends Controller
{
public function delete($id) {
$toDelete = Attachment::findOrFail($id);
$toDelete->delete();
return redirect()->back();
}
}
In the CategoryController (edit method), I fetch the attachments linked to each category to be rendered inside the view:
public function edit($category)
{
$wildcard = $category;
$category = Category::findOrFail($wildcard);
$attachments = App\Category::findOrFail($wildcard)->attachments()->get()->toArray();
return view('category.edit', [
'category' => $category,
'attachments' => $attachments
]);
}
In the view, I render the attachment and the button to delete. I am fully aware of the error of having a form inside another form, nevertheless I do not know antoher approach to submit this delete request.
// update Category form
#foreach ($attachments as $attachment)
<div class="row">
<div class="col-4">
<img style="width: 100%;" src={{ $attachment['url'] }} alt="">
</div>
<div class="col-4">
<div>
<p class="general_par general_url">{{ $attachment['url'] }}</p>
</div>
</div>
<div class="col-4">
<form action="{{ route('attachment', $attachment['id']) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete Image</button>
</form>
</div>
</div>
<hr>
#endforeach
// end of update Category form
Should I build a deleteAttachment method inside the CategoryController? If so, how can I still submit the Delete request? Also, if any other Model in the future will have attachments, should I build a deleteAttachment method inside each controller? That is cumbersome. Thanks in advance
if you don't like to use form, then use tag:
<a class="btn btn-danger" href="{{ route('attachment', $attachment['id']) }}">Delete Image</a>
And redefine the route to Route::get(...)
(or maybe use ajax for POST method if that is required)

Resources