I created a resource controller. The problem is when I code a route in my view, the browser displays Route [crops.restore] not defined. I run the php artisan route:list command and I realize that the route does not exist in the route list.
My controller is this
// Display all crops
public function index(Request $request)
{
if ($request->has('trashed')) {
$records = Crop::onlyTrashed()
->get();
} else {
$records = Crop::get();
}
return view('crops.index', compact('records'));
}
//restore a file
public function restore(Request $request, $id)
{
Crop::withTrashed()->find($id)->restore();
return redirect()->back();
}
My route
// Crop controllers
Route::resource('crops', 'CropsController');
My View
#foreach ($records as $crops)
//code ...
#if(request()->has('trashed'))
<a class="btn btn-success btn-sm float-left mr-1" href="/crops/{{ $crops['id'] }}/edit">Edit</a>
Restore
#else
<form method="POST" class="float-left" action="{{ route('crops.destroy', $crops->id) }}">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-danger delete btn-sm " title='Delete'>Delete</button>
</form>
#endif
#endforeach
What's wrong with my code?
Restore provide this methods in your controller: index, store, edit, update, show, destroy. If you want redirect to your custom method like restore your have to rewrite your route in this matter:
Route::get('/restore', [App\Http\Controllers\CropsController::class, 'restore']);
Just add the route in the web.php file. For example:
Route::get('/restore', '\App\Http\Controllers\HomeController#restore');
Related
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 have a form associated with the destroy method to delete a item/record.
<form action="{{ route('climb-excluded.destroy',$exclude->id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-danger btn-sm">
<i class="fa fa-trash-o" aria-hidden="true"></i> Delete
</button>
</form>
destroy() method
<?php
public function destroy(Cexcluded $cexcluded)
{
$cexcluded->tours()->detach();
$cexcluded ->delete();
Session::flash('success','Item sucessfully deleted !');
return redirect()->route('climb.ie');
}
Routes
Route::resource('climb-excluded','CexcludedsController', ['only'=>['store','destroy']]);
Route::get('climbing/included-excluded','HomeController#getclimbIE')->name('climb.ie');
The trouble I'm having is the destroy method is not deleting the record from the DB and doesn't give any error. It is giving a session message without deleting the record.
I saw your other thread with the same issue regarding the detach causing problems.
Add this to your Cexcluded model
public static function boot()
{
parent::boot();
static::deleting(function($model) {
$model->tours()->detach();
});
}
Remove the following line from your controller method
$cexcluded->tours()->detach();
So try this
public function destroy(Cexcluded $cexcluded)
{
$cexcluded ->delete();
Session::flash('success','Item sucessfully deleted !');
return redirect()->route('climb.ie');
}
You must use "->destroy()" in the controller.
Check this question for clarification between destroy() and delete()
I have a problem with laravel and deleting.
Basically this should delete a row from database but when i click on delete it redirects me back but it doesn't delete what it should
Here is the code im working on
<div class="list-group">
#forelse($categories as $category)
<h4>{{$category->name}}</h4>
<form action="{{route('categories.destroy', $category->id)}}" method="POST" class="inline-it">
{{csrf_field()}}
{{method_field('DELETE')}}
<input class="btn btn-xs btn-danger" type="submit" value="Delete">
</form>
<hr>
#empty
<h5> - No categories.</h5>
#endforelse
</div>
route:
Route::resource('/categories','CategoriesController');
controller:
public function destroy(Categories $categories)
{
$categories->delete();
return back();
}
edit dd https://pastebin.com/s41djcUH
edit
Just change $categories with $category
public function destroy(Categories $category)
{
$category->delete();
return back();
}
For Implicit Route Model Binding:
https://laravel.com/docs/master/routing#route-model-binding
Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.
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!');
}
}
Code in my Route file look like this :
Route::delete('/subtask1/delete/{{subtask}}', 'TaskController#subtaskdestroy');
Route::get('/home', 'HomeController#index');
Route::get('/redirect/{provider}', 'SocialAuthController#redirect');
Route::get('/callback/{provider}', 'SocialAuthController#callback');
});
Code in view file:
<form action="/subtask1/delete/{{1}}" method="POST" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" id="delete-task-{{$subtask->id }}" class="btn btn-danger btn-xs">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
And code on the Controller:
public function subtaskdestroy(Request $request, Subtask $subtask)
{
$this->authorize('checkTaskOwner', $subtask);
$subtask->delete();
return redirect('/tasks');
}
With this code, I am getting an error like this:
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 161:
You mistake when defined route for delete. It should be like this:
Route::delete('/subtask1/delete/{subtask}', 'TaskController#subtaskdestroy');
But you given:
Route::delete('/subtask1/delete/{{subtask}}', 'TaskController#subtaskdestroy');
More about Route Parameters:
Laravel Route Parameters
You used return redirect('/tasks'); in your controller. With this line the page will redirecte to route /tasks after successfully delete the data. Make sure you have /tasks route in your route file. Example:
Route::get('/tasks','YourController#method');