Deleting product data - crud of products

Step 01: In ProductController, adding destroy() method

public function destroy( Product $product ) {
	$product->delete();
	$redirectTo = session()->get('submit_redirect');
	if( !empty( $redirectTo ) ) {
		return redirect(url('').$redirectTo)->with( 'success', 'Product deleted successfully' );
	}
	return redirect()->route( 'products.index' )->with( 'success', 'Product deleted successfully' );
} 

Above session variable ( submit_redirect ) is used to keep current url query string so that if any product data is deleted on page 2 or 3, after this deletion redirection will occur into that current path which is saved into that session variable.

Step 02: So to save this path to session variable we need to change index() method like below

public function index( Request $request ) { 
	$meta_title = 'All Products';    
	
	$query = Product::query();    

	//used in delete() method for correct page redirection
	session('submit_redirect', '');
	if($request->query->count() > 0) { // if query string has any value count will be greater than 0
		session()->put('submit_redirect', $_SERVER['REQUEST_URI']); //getting current query string from url and saving it to session variable
	}
	$products = $query->orderBy( 'sequence', 'desc' )->paginate( 10 )->withQueryString();   
	return view( 'products.list', compact( 'meta_title','products' ) );
}

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Adding user data - crud of users

Editing user data - crud of users

Deleting user data - crud of users

Listing rows of products - crud

Listing rows of profiles - crud

Listing rows of orders - crud

Listing rows of order items - crud