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.
Related
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');
i want to do seller can delete the product
<form action="{{ route('product.destroy'}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
this is in web.php
Route::get('/index', 'ProductController#index');//seller view all product
Route::get('/create', 'ProductController#create'); //seller create new product
Route::post('','ProductController#store')->name('product.store'); //store in database
Route::get('/edit/{id}','ProductController#edit'); // seller edit post
Route::put('edit/{id}','ProductController#update')->name('product.update'); //seller update
Route::delete('/{id}','ProductController#destroy')->name('product.destroy');//seller delete product
this is in ProductController
public function destroy($id)
{
$product= Product::find($id);
Storage::delete($product->image);
$product->delete();
return back()->withInfo('Product has been deleted');
}
HELP me please
I think you are missing ID do it like
{{ route('product.destroy', $product->id)}}
you can try removing the post method of the form cause you specify another one and depend on which version you are using you can try #method('DELETE')
#method('DELETE')
it just more elegant
Pass the ID in the route in form tags,
{{ route('product.destroy', $product->id)}}
Also
$product= Product::find($id);
Storage::delete($product->image);
$product->delete();
Check if their is a $product, then delete it, otherwise you'll get an error.
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 am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
I'm trying to delete a single record by id. Instead, it deletes all records in that table.
Here's my code:
View
<form role="form" action="{{ route('status.delete', ['statusId' => $status->id]) }}" method="post">
<button type="submit" class="btn btn-default"><i class="fa fa-times"></i> Delete</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
Routes
Route::post('/status/{statusId}/delete', [
'uses' => '\Dashboard\Http\Controllers\StatusController#deleteStatus',
'as' => 'status.delete',
'middleware' => ['auth'],
]);
Controller
public function deleteStatus(Request $request, $statusId)
{
Auth::user()->statuses()->delete($statusId);
return redirect()->route('home')->with('success', 'Post deleted.');
}
Note: When I dd($statusId) it does provide the right ID for the status I'm deleting. So that part does work.
This is possible in Laravel 5.6 using the destroy method:
From the docs:
However, if you know the primary key of the model, you may delete the
model without retrieving it. To do so, call the destroy method
App\Model::destroy(1);
or to delete an array of ids:
App\Model::destroy([1, 2, 3]);
or by query:
App\Model::where('active', 0)->delete();
Unfortunately, the Eloquent builder does not support passing the id to delete.
Instead, you have to first find to model, then call delete on it:
$request->user()->statuses()->findOrFail($statusId)->delete();
you can delete the model by using another approach like
App\Models\ModelName::find(id)->delete()
but it throws nullPointerException that you have to handle
**Step 1 create route inside web.php**
Route::delete('/answers_delete/{id}', [App\Http\Controllers\AnswerController::class, 'delete'])->name('answers.delete');
**Step 2 Create method in your controller**
use App\Models\Answer; // use in top of this file
public function delete($id)
{
$ans = Answer::find($id);
$ans->delete();
session()->flash('success', 'Answer Deleted Successfully!!!');
return view('admin.anser.index');
}
**Step 3 define your route name inside form action(Note my case view file name index.blade.php and inside admin/answer/index.blade.php)*
<form action="{{ route('answers.delete', $answer->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger" style="display: inline;">Delete</button>
</form>