The 'Delete' method was not working so I used 'get' but my data is not deleting
<form method="post" class="delete_form" action="{{ url('/data', $row['id']) }}" id="studentForm_{{$row['id']}}">
{{ method_field('GET') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
</form>
public function destroy($id)
{
dd($id);
$student = Student::where('id',$id)->delete();
return redirect('/data/{id}')->with('success', 'Data Deleted');
}
}
Route::get('/data/{id}','App\Http\Controllers\StudentController#index');
Route::delete('/data/{id}','App\Http\Controllers\StudentController#destroy');
This is I finally got the answer
I ran the following command:
php artisan route:clear
php artisan route:list
Then it gave me suggested methods and delete method was shown for "Controller#destroy" route then I changed from "get" method to "delete" method. Then, my post was able to delete.
The form must be submitted via delete method
<form method="POST" action="{{ url('/data', $row['id']) }}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
</form>
More details can be found here: https://laravel.com/docs/8.x/routing#form-method-spoofing
Related
In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.
I'm doing this:
The view
#if ($message->readed != 0)
<p class="card-text message text-left">{{ $message->message }}</p>
#else
<form method="POST" action="/message/read">
#csrf
#method('PATCH')
<input type="hidden" name="message" value="{{ $message->id }}"/>
<button class="btn btn-info text-white" type="submit">
Leggi
</button>
</form>
#endif
The route in web.php
Route::patch('message/read', 'MusicianController#readMessage');
The function
public function readMessage(Request $request)
{
$message = Message::where('id', $request->id)->first();
$message->readed = 1;
$message->update();
return redirect()->back()->with('message', 'message updated');
}
But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.
Even if I had specified a patch method in routes and even in the form with #method('PATCH')
Could someone help me understand what's wrong please??
the main answer
your route is:
Route::patch('message/read', 'MusicianController#readMessage');
replace your route with following route that use for all CRUD opration:
Route::resource('message/read', 'MusicianController');
if you use ajax for submit data then replace your type and url with following:
type: "patch",
url: "{{url('message/read')}}",
if you don't use ajax than use following:
<form method="POST" action="{{url('message/read"')}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
update: after version 5.6 you can use these syntax for above functions in any blade file:
<form method="POST" action="{{url('message/read"')}}">
#csrf
#method('PATCH')
</form>
I have created route with "resource". When I try to use delete method it always going to show method.
Route list
Route call
<a class="btn btn-danger" href="{{ route('languages.destroy', ['language' => $language->id]) }}">Delete</a>
Delete method
public function destroy($language){
$lang = Language::findOrFail($language);
$lang->delete();
session()->flash('flash_message', 'The language has been
removed!');
return redirect(route('languages.index'));
}
So how to fix it?
Thank you!
Since it goes to GET method because you are not deleting using form .
route('languages.destroy',['language' => $language->id])
the above route only generate url .so if you are using
delete
then it treat as get method.So you have to use
<form method="POST" action="{{ route('languages.destroy',['language' => $language->id]) }}">
#csrf
#method("delete")
<button type="submit">Delete</button>
</form>
In your blade:
<form action="{{ route('languages.destroy',$language->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
I have this basic error but i cant fix it... can I have some help please ?
This is my view, and I tried with tokens #csrf and also #csrf-field and token.
I tried to write Post, post, POST.
(prat.store work well, the problem is update.)
#if(isset($ModificationMode))
<form method="post" action="{{route('prat.update', $DataPraticien ?? '')}}">
#csrf
#else
<form action="{{route('prat.store')}}" method="post">
#endif
//stuff
//stuff
/lalala
#if(isset($ModificationMode))
<button type="submit" class="btn btn-warning">Modifier Praticien</button>
#else
<button type="submit" class="btn btn-success">Ajouter Praticien</button>
#endif
my controller :
public function update(Request $request, $id)
{
$ModifPrat= Praticien::find($id);
$ModifPrat->NOM = $request->input('NOM');
$ModifPrat->ETAT_CIVIL = $request->input('ETAT_CIVIL');
$ModifPrat->NOTE = $request->input('NOTE');
$ModifPrat->NOTORIETE = $request->input('NOTORIETE');
$ModifPrat->MENBRE_ASSOCIATION = $request->input('MEMBRE_ASSOCIATION');
$ModifPrat->DIPLOME = $request->input('DIPLOME');
$ModifPrat->save();
return redirect()->route('homeAdmin', auth()->id());
}
My route is a basic resource :
Route::resource('prat', 'PratController');
NB : The variable ModificationMode is a way to use the same Page for two distinct task. I used var_dump to debug it and the variable is set well and my prat.update is detected.
Thanks ;)
If you're modifying, use this
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
you can refer to Form Method Spoofing or Resource Controllers
Check out the documentation for ResourceControllers. There is a table that explains how Laravel maps the controller methods to the request types. Basically, you need to use the #method directive (or manually add the hidden input). So, your form should look like:
<form action="{{ route('prat.update') }}" method="POST">
#method('PUT')
//...
</form>
I want to delete bookings this is my form
<form action="{{ route('bookings.delete', $booking->id) }}" method="POST">
#csrf
#method('delete')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
this is my controller
public function delete($id){
$booking = Booking::find($id);
$booking->delete();
}
and this is my route
Route::post('/bookings/delete', 'BookingController#delete')->name('bookings/delete');
Your route should be defined as
Route::delete('/bookings/delete/{id}', 'BookingController#delete')->name('bookings.delete');
You should also pass the id as a route parameter
In the delete method of your controller
public function delete($id)
{
$booking = Booking::findOrFail($id);
$booking->delete();
return redirect()->route('bookings.index');
}
In your route file
name('bookings/delete');
Should be
name('bookings.delete');
Try it and let us know
if you need to use #method('delete') then your route should be delete() not post
Route::delete('/bookings/delete/{id}', 'BookingController#delete')->name('bookings.delete');
And form:
<form action="{{ route('bookings.delete', $booking->id) }}" method="POST">
#csrf
#method('delete')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
Destroy method not working
I am trying to delete an image from my database but I am receiving the following error:
Undefined variable: portfolio (View: C:\MyProjects\bubblehouseProject\resources\views\admin\portfolio\addPortfolio.blade.php)
I believe there is something wrong with the logic I am trying to implement in my Laravel application. I am using Eloquent ORM.
Controller:
public function Destroy($id) {
$portfolio = new Portfolio();
$portfolio::find($id);
$portfolio->delete();
return redirect('addPortfolio')->with('delete', 'The image has been successfully deleted!');
}
Route:
Route::get('addPortfolio/{id}', 'AddPortfolioController#Destroy');
View:
<form action="{{ route('addPortfolioController.Destroy', $portfolio->id) }}">
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
{{ method_field('DELETE') }}
</form>
I am not entirely sure that this is the right way to delete data from the database.
Route
Route::delete('addPortfolio/{id}', 'AddPortfolioController#Destroy')->name('portfolio.destroy');
View
<form action="{{ route('portfolio.destroy', $portfolio->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
</form>
Controller
public function destroy($id)
{
if(Portfolio::destroy($id)) {
return redirect('addPortfolio')->with('success', 'The image has been successfully deleted!');
} else {
return redirect('addPortfolio')->with('error', 'Please try again!');
}
}