This code is deleting my post but new post is creating on new id (i.e we delete post from id 2 the new post will publish with id 3 the id is not deleting)
public function delete( $id){
$post = Post::find($id);
$post::where('id', $id)->first()->delete();
// $post->destroy($id);
return redirect('home/faqs');}
Controller
Route::get('/post/delete/{id}','PostController@delete')->name('post.delete');
Link
<a href="{{ route('post.delete', $post->id) }}" >Delete</a>
CodePudding user response:
This is correct. Databases do not reset the primary key increment when a row is deleted as that is just not the way standard databases work.
Logically in your case - it would be easy to delete id: 3
and then set the auto increment back to 3, but what if you have 10 posts, and you delete the 4th one?
If you set the increment id back to 4 in this case, you will be able to create one row, and then run into an issue with a duplicate primary key, which will only be able to be fixed by you manually setting the auto increment above 10.
CodePudding user response:
Hi you can do it this way.
HTML
<a onclick="$('#delete_{{ $single->id }}').submit()" >Delete</a><form id="delete_{{ $single->id }}" action="{{ route('post.destroy', $single->id) }}" method="POST">
@csrf
<input type="hidden" name="_method" value="DELETE">
Controller
public function destroy($id)
{
Post::find($id)->delete();
return redirect('home/faqs')->with(['success'=>'PostDeleted Successfully']);
}