Reputation: 519
This should be a simple task I am just not fully grasping laravel yet. I have my controllers view and models setup. I want to use my users.destroy route to delete my row in the db. But I want to do it a certain way. I want to have an alert show In my alert area on my page asking to confirm the deletion of a certain user. Im assuming I need to pass the user id in a session to an alert to confirm my delete on a delete button click. Click 1 button to open an alert on the top of my page if I click confirm it calls user.destroy.
View:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>View All Users</h4>
@if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
@if(session()->get('danger'))
<div class="alert alert-danger">
{{ session()->get('danger') }}
</div>
@endif
</div>
<div class="card-body">
<div class="text-center my-2">
<a href="{{ route('register') }}" class="btn btn-primary">New User</a>
</div>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->username}}</td>
<td class="text-center">
<a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a>
<a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a>
<a href="#" class="btn btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
Controller:
public function destroy($id)
{
User::find($id)->delete();
return redirect()->route('users.index')->with('success','User Deleted');
}
Route:
Route::resource('users', 'UserController');
Upvotes: 1
Views: 14546
Reputation: 3318
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>View All Users</h4>
@if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
@if(session()->get('danger'))
<div class="alert alert-danger">
{{ session()->get('danger') }}
</div>
@endif
</div>
<div class="card-body">
<div class="text-center my-2">
<a href="{{ route('register') }}" class="btn btn-primary">New User</a>
</div>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->username}}</td>
<td class="text-center">
<a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a>
<a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a>
<a href="javascript:;" rel="{{ route('users.delete', $user->id) }}" class="btn btn-danger deleteUser">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).on('click','.deleteUser',function() {
var url = $(this).attr('rel');
if(confirm("Are you sure you want to delete this?")){
window.location.href = url
}
else{
return false;
}
})
</script>
or
you can use any plugin like
http://myclabs.github.io/jquery.confirm/
http://bootboxjs.com/
In controller
```php
public function deleteposts(Request $request)
{
// your delete code is here
$request->session()->flash('success', 'Post deleted sucessfully');
return redirect()->back();
}
here you setting success message
Upvotes: 0
Reputation: 6341
Its Just An Example i am considering your model as User.php
If You want icon just add the font awesome css
Open Your User.php
Model and paste the below code
/**
* @function tableActionButtons
* @author Manojkiran <[email protected]>
* @param string $fullUrl
* @param integer $id
* @param string $titleValue
* @param array $buttonActions
* @usage Generates the buttons
* @version 1.0
**/
/*
NOTE:
if you want to show tooltip you need the JQUERY JS and tooltip Javascript
if you are not well in JavaScript Just Use My Function toolTipScript()
|--------------------------------------------------------------------------
| Generates the buttons
|--------------------------------------------------------------------------
|Generates the buttons while displaying the table data in laravel
|when the project is bigger and if you are laravel expert you this.
|But if you are the learner just go with basic
|
|Basically It Will generate the buttons for show edit delete record with the default
|Route::resource('foo',FooController);
|
|//requirements
|
|//bootstrap --version (4.1.3)
|// <link rel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="" crossorigin="">
|//fontawesome --version (5.6.0(all))
|//<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.0/css/all.css" integrity="" crossorigin="">
|
|if you want to show tooltip you nee the jquery and tooltip you need these js and toottipscript javascript or use my function toolTipScript
|
|//jquery
|// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|//popper js
|// <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
|//bootstrap js
|// <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
|
|usage
|option1:
|tableActionButtons(url()->full(),$item->id,$item->name);
|this will generate all the buttons
|
|option2:
|tableActionButtons(url()->full(),$item->id,$item->name,['edit',delete]);
|this will generate edit and delete the buttons
|
|option3:
|tableActionButtons(url()->full(),$item->id,$item->name,['edit',delete,delete],'group');
|this will generate all the buttons with button grouping
|
|option4:
|tableActionButtons(url()->full(),$item->id,$item->name,['show','edit','delete'],'dropdown');
|this will generate all the buttons with button dropdown
|
*/
public static function tableActionButtons($fullUrl, $id, $titleValue, $buttonActions = ['show', 'edit', 'delete'], $buttonOptions = '', $encryptId = false)
{
$fullUrl = strtok($fullUrl, '?');
if ($encryptId) {
$id = Crypt::encrypt($id);
}
// dd(get_class_methods(HtmlString::class));
//Value of the post Method
$postMethod = 'POST';
//if the application is laravel then csrf is used
$token = csrf_token();
//NON laravel application
// if (function_exists('csrf_token'))
// {
// $token = csrf_token();
// }elseif (!function_exists('csrf_token'))
// //else if the mcrypt id is used if the function exits
// {
// if (function_exists('mcrypt_create_iv'))
// {
// // if the mcrypt_create_iv id is used if the function exits the set the token
// $token = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
// }
// else{
// // elseopenssl_random_pseudo_bytes is used if the function exits the set the token
// $token = bin2hex(openssl_random_pseudo_bytes(32));
// }
// }
//action button Value
//(url()->full()) will pass the current browser url to the function[only aplicable in laravel]
$urlWithId = $fullUrl . '/' . $id;
//Charset UsedByFrom
$charset = 'UTF-8';
// Start Delete Button Arguments
//title for delete functions
$deleteFunctionTitle = 'Delete';
//class name for the deletebutton
$deleteButtonClass = 'btn-delete btn btn-xs btn-danger';
//Icon for the delete Button
$deleteButtonIcon = 'fa fa-trash';
//text for the delete button
$deleteButtonText = 'Delete';
//dialog Which needs to be displayes while deleting the record
$deleteConfirmationDialog = 'Are You Sure you wnat to delete ' . $titleValue;
$deleteButtonTooltopPostion = 'top';
// End Delete Button Arguments
// Start Edit Button Arguments
//title for Edit functions
$editFunctionTitle = 'Edit';
$editButtonClass = 'btn-delete btn btn-xs btn-primary';
//Icon for the Edit Button
$editButtonIcon = 'fa fa-edit';
//text for the Edit button
$editButtonText = 'Edit';
$editButtonTooltopPostion = 'top';
// End Edit Button Arguments
// Start Show Button Arguments
//title for Edit functions
$showFunctionTitle = 'Show';
$showButtonClass = 'btn-delete btn btn-xs btn-primary';
//Icon for the Show Button
$showButtonIcon = 'fa fa-eye';
//text for the Show button
$showButtonText = 'Show';
$showButtonTooltopPostion = 'top';
// End Show Button Arguments
//Start Arguments for DropDown Buttons
$dropDownButtonName = 'Actions';
//End Arguments for DropDown Buttons
$showButton = '';
$showButton .= '
<a href="' . $fullUrl . '/' . $id . '"class="' . $showButtonClass . '"data-toggle="tooltip"data-placement="' . $showButtonTooltopPostion . '"title="' . $showFunctionTitle . '-' . $titleValue . '">
<i class="' . $showButtonIcon . '"></i> ' . $showButtonText . '
</a>
';
$editButton = '';
$editButton .= '
<a href="' . $urlWithId . '/edit' . '"class="' . $editButtonClass . '"data-toggle="tooltip"data-placement="' . $editButtonTooltopPostion . '" title="' . $editFunctionTitle . '-' . $titleValue . '">
<i class="' . $editButtonIcon . '"></i> ' . $editButtonText . '
</a>
';
$deleteButton = '';
$deleteButton .= '
<form id="form-delete-row' . $id . '" method="' . $postMethod . '" action="' . $urlWithId . '" accept-charset="' . $charset . '"style="display: inline" onSubmit="return confirm("' . $deleteConfirmationDialog . '")">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="' . $token . '">
<input name="_id" type="hidden" value="' . $id . '">
<button type="submit"class="' . $deleteButtonClass . '"data-toggle="tooltip"data-placement="' . $deleteButtonTooltopPostion . '" title="' . $deleteFunctionTitle . '-' . $titleValue . '">
<i class="' . $deleteButtonIcon . '"></i>' . $deleteButtonText . '
</button>
</form>
';
// $deleteButton = "<a href='index.php?page=de_activate_organization&action_id=$id' onClick=\"return confirm('Are you Sure to De Activate?')\"><span class='label label-success'>" ."Test" . "</span></a>";
$actionButtons = '';
foreach ($buttonActions as $buttonAction) {
if ($buttonAction == 'show') {
$actionButtons .= $showButton;
}
if ($buttonAction == 'edit') {
$actionButtons .= $editButton;
}
if ($buttonAction == 'delete') {
$actionButtons .= $deleteButton;
}
}
if (empty($buttonOptions)) {
return new HtmlString($actionButtons);
} elseif (!empty($buttonOptions)) {
if ($buttonOptions == 'group') {
$buttonGroup = '<div class="btn-group" role="group" aria-label="">
' . $actionButtons . '
</div>';
return new HtmlString($buttonGroup);
} elseif ($buttonOptions == 'dropdown') {
$dropDownButton =
'<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
' . $dropDownButtonName . '
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
' . $actionButtons . '
</div>
</div>
';
return new HtmlString($dropDownButton);
} else {
return 'only <code>group</code> and <code>dropdown</code> is Available ';
}
}
}
Now add this to User.php
use Illuminate\Support\HtmlString;
Now Open Your List index.blade.php
and inside the for loop iteration add the below line
{{ App\User::tableActionButtons(url()->full(),$user->id,$user->name,['delete'],null,false) }}
If You want Multiple Buttons 4rt argument Accepts Array
{{ App\User::tableActionButtons(url()->full(),$user->id,$user->name,['show','edit,'delete],null,false) }}
If You face any issue kindly comment below
Hope it helps
Upvotes: 0
Reputation: 1763
Always try to use DELETE method for delete resource that is best way and practice
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>View All Users</h4>
@if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
@if(session()->get('danger'))
<div class="alert alert-danger">
{{ session()->get('danger') }}
</div>
@endif
</div>
<div class="card-body">
<div class="text-center my-2">
<a href="{{ route('register') }}" class="btn btn-primary">New User</a>
</div>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->username}}</td>
<td class="text-center">
<a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a>
<a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a>
<form method="POST" action="{{ route('users.delete', $user->id) }}">
@csrf // or hidden field
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-xs btn-danger btn-flat show_confirm" data-toggle="tooltip" title='Delete'> <i class="fa fa-trash"> </i></button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$('.show_confirm').click(function(e) {
if(!confirm('Are you sure you want to delete this?')) {
e.preventDefault();
}
});
</script>
If you are using Laravel collective HTML then you can replace that form tag
Upvotes: 2
Reputation: 11
You can simply use an onclick function in your tag like
<a href="{{ route('users.delete', $user->id) }}" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this usere?');">Delete</a>
Upvotes: 1
Reputation: 724
Add below link for delete record
<a href="{{ route('users.delete', $user->id) }}" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>
Upvotes: 0