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>
Related
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
I've just created a new project something like todo list in Laravel. When I try to do simple deleting I get this error:
Missing required parameters for [Route: destroy] [URI: {}]. (View: C:...\resources\views\index.blade.php)
Here is part of code from index.blade.php:
#if($todos)
<ol>
#foreach($todos as $todo)
<li>{{ $todo->todo }}</li>
<form action="" method="post">
#csrf
#method('Delete')
x
</form>
#endforeach
</ol>
#endif
so I am just checking if there is anything, if not, then don't show list.
Part of code from controller:
public function index()
{
$todos = Todo::all();
return view('index', ['todos' => $todos]);
}
public function destroy($id)
{
Todo::findOrFail($id)->delete();
}
and line of code from web.php:
Route::resource('/', 'TodosController');
This is so basic and it is making me crazy because I can't figure out what is causing this error. Seems like everything is good.
Im not 100% certain, but try updating your resource route declaration to:
Route::resource('todos', 'TodosController');
and changing your link to
x
In the docs here, https://laravel.com/docs/5.7/controllers#restful-naming-resource-route-parameters
They describe the routes as being created like:
Actions Handled By Resource Controller
DELETE /photos/{photo} destroy photos.destroy
You don't actually submit your form. You just click on a link, so you in essence make a GET request, and not a POST request.
You need to send it through a form.
#foreach($todos as $todo)
<li>{{ $todo->todo }}</li>
<form action="{{ route('destroy', $todo->id) }}" method="POST">
#csrf
#method('DELETE')
<input type="submit" class="btn btn-danger" value="x" />
</form>
#endforeach
Having an inline form like that can appear a bit weird, so you can instead put the button outside, and submit your form on click.
#foreach($todos as $todo)
<li>
{{ $todo->todo }}
<a href="#" class="btn btn-danger"
onclick="event.preventDefault();
document.getElementById('todo-destroy-{{ $todo->id }}').submit();">x</a>
</li>
<form action="{{ route('destroy', $todo->id) }}" method="POST" id="todo-destroy-{{ $todo->id }}">
#csrf
#method('DELETE')
</form>
#endforeach
I tried to delete some file of my database from the aplication using a destroy function but doesn't work, it works with routes that are simples but no composed,its for school
Below i will let the code of my route, the destroy function() , how i tried to invocate the function and my index function()
Give me the Error:Missing required parameters for [Route: asociados.destroy] [URI: eventos/{eventos}/miembros/{miembros}/asociados/{asociado}]
Route
Route::resource('/eventos/{eventos}/miembros/{miembros}/asociados', 'miembroController');
Destroy function()
public function destroy($id)
{
$miembro=Miembro::find($id);
$miembro->delete();
return back()->with('Evento eliminado');
}
* Index function()*
public function index(Request $request, $id_evento,$id_miembro){
$miembros = DB::select(DB::raw(
"SELECT id_miembro, razon_social, denominacion_comercial, web,
rif
FROM miembro
" ));
return view ('home.miembro')->with('miembros', $miembros)->with('id_evento', $id_evento)->with('id_miembro', $id_miembro);
}
Code in the html of how i tried to invocate the destroy function()
<td>
<form action={{ route('asociados.destroy', ['miembro' => $item->id_miembro]) }}
method="POST" class="d-inline">
#csrf
#method('DELETE')
<button class="btn btn-dark btn-sm" type="submit">Eliminar</button>
</form>
</td>
You have to give to the route helper also the event id:
<td>
<form action={{ route('asociados.destroy', ['eventos'=> $id_evento ,'miembro' => $item->id_miembro]) }}
method="POST" class="d-inline">
#csrf
#method('DELETE')
<button class="btn btn-dark btn-sm" type="submit">Eliminar</button>
</form>
</td>
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>
I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
result page
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
Route::get('/dashboard/result/generatePDFpage', 'resultController#GeneratePDFc');
GeneratePDFc controller
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
Could someone please help me out? :-)
When you're using get method, you can do just this:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE method):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;
I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request var