Reputation: 1828
I'm fairly new to Laravel and would like to create a functionality where I will delete a single record from the database by clicking a link.
Below is the code on the view:
@foreach($companies as $company)
<tr>
<td><a href="/companies/{{$company->id}}"> {{$company->name}}</a></td>
<td><a href="/companies/{{$company->id}}"> {{$company->descriptions}}</a></td>
<td><a href="/companies/{{$company->id}}"> {{$company->comptype->name}}</a></td>
<td>
<a href="{{route('companies.edit', $company->id)}}" class="btn btn-xs btn-default"><i class="fa fa-edit"></i></a>
<a onclick="
var result = confirm('Are you sure you wish to delete this Company?');
if( result ){
event.preventDefault();
document.getElementById('delete-form').submit();
}
"
class="btn btn-xs btn-default"><i class="fa fa-trash"></i> </a>
<form id="delete-form" action="{{ route('companies.destroy',[$company->id]) }}"
method="POST" style="display: none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
</td>
</tr>
@endforeach
Next is the destroy method in the Controller
public function destroy(Company $company)
{
//
// dd($company);
$findCompany = Company::findOrFail( $company->id);
if($findCompany->delete()){
//redirect
return redirect()->route('companies.index')
->with('success' , 'Company deleted successfully');
}
return back()->withInput()->with('error' , 'Company could not be deleted');
}
When clicking on trash, to delete a particular record, the record on the table gets deleted and it doesn't matter which I try to delete, the first record always gets deleted. When I did a diedown, I found that the URL has the correct record but dd shows that the first record will be deleted. I am fairly new to Laravel, I've done this using Laravelcollective/html, but unfortunately it's not compatible with Laravel 5.5.28. Please be so kind to assist
Upvotes: 1
Views: 676
Reputation: 163898
The problem is in JS code. Your JS code always sumbits the same delete form. To fix this, you need to use unique IDs:
<form id="delete-form{{ $company->id }}"
And then:
document.getElementById('delete-form{{ $company->id }}').submit();
Also, mixing JS and PHP is a bad practice. You should really put all the JS into a separate JS file.
Upvotes: 3
Reputation: 31
Try this code for view file :
<div class="col-xs-12">
<a href="{{url('/delete')}}/{{$allDemos[$i]->id}}" class='btndelete'>
<i class="fa fa-trash"></i>
</a>
SliderControler.php :
public function delete($id){
$Slider = Slider::find($id);
unlink('slider_images/'.$Slider->slider_img);
$Slider->delete();
return redirect('/slider');
}
Upvotes: 1
Reputation: 414
Try this
see the form action "{{ route('companies.destroy',[$company->id]) }}"
to {{ url('companies/destroy/'.$company->id) }}
see the destroy methos in wrong
Click the link
Perfect Method
public function destroy($id){
$findCompany = Company::findOrFail($id);
if($findCompany->delete()){
//redirect
return redirect()->route('companies.index')
->with('success' , 'Company deleted successfully');
}
return back()->withInput()->with('error' , 'Company could not be deleted');}
Upvotes: 1
Reputation: 1056
Try That way it's my project code :
View File :
<td>
<form action="{{action('TagController@destroy', $row['id'])}}" method="post">
{{csrf_field()}}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
TagController.php:
public function destroy($id)
{
$tags = Tag::find($id);
$success = $tags->delete();
if($success)
{
return redirect('/admin/tag')
->with('success','Tag deleted.');
}
return back()->withInput()->with('errors','Some error...');
}
Upvotes: 1
Reputation: 11656
Try this:
public function destroy(Company $company)
{
if($company->delete()){
//redirect
return redirect()->route('companies.index')
->with('success' , 'Company deleted successfully');
}
return back()->withInput()->with('error' , 'Company could not be deleted');
}
Upvotes: 0