Reputation: 5
I was testing my delete function but how do I integrate it to a button Here is the code
profile.blade
<div class="modal-body">Select the delete button to continue</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" data-dismiss="modal">Cancel</button>
<button class="btn btn-secondary"type="button" href="/delete">Delete</a>
</div>
</div>
This is where I wanted to use the function
Here is the routes
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{
DashboardController,
HomeController,
LoginController,
RegisterController,
FileuploadController,
MemberController
};
Route::middleware(['guest'])->group(function() {
// (url_attern, [Controller_name, method_name])->name(alias)
Route::get('/', [HomeController::class, 'index'])->name('home.index');
Route::get('/attorney', [HomeController::class, 'attorney'])->name('home.attorney');
Route::get('/service', [HomeController::class, 'service'])->name('home.service');
Route::get('/login', [LoginController::class, 'login'])->name('login.login');
Route::post('/login', [LoginController::class, 'auth'])->name('login.auth');
Route::get('/register', [RegisterController::class, 'signin'])->name('register.signin');
Route::post('/register', [RegisterController::class, 'register'])->name('register.register');
});
Route::middleware(['auth'])->group(function() {
Route::get('/logout', [LoginController::class, 'logout'])->name('login.logout');
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard.dashboard');
Route::get('/profile', [DashboardController::class, 'profile'])->name('dashboard.profile');
Route::get('/journal', [DashboardController::class, 'journal'])->name('dashboard.journal');
Route::get('/files', [DashboardController::class, 'files'])->name('dashboard.files');
Route::post('/files', [FileuploadController::class, 'files'])->name('dashboard.files');
Route::get('/directory', [DashboardController::class, 'directory'])->name('dashboard.directory');
Route::get('/calendar', [DashboardController::class, 'calendar'])->name('dashboard.calendar');
Route::post('/delete', [DashboardController::class, 'delete'])->name('dashboard.delete');
});
And the function
public function delete(){
$user = \User::find(Auth::user()->id);
Auth::logout();
if ($user->delete()) {
return Redirect::route('/')->with('global', 'Your account has been deleted!');
}
}
How do I use the function in order to delete the user currently logged in My route is already at post for the delete but it tells that the GET method is not supported for this route
Upvotes: 0
Views: 72
Reputation: 116
The route is from the POST verb, so you need to insert the button inside a form:
<div class="modal-body">Select the delete button to continue</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" data-dismiss="modal">Cancel</button>
<form action="{{ route('dashboard.delete') }}" method="POST">
{{ csrf() }}
<button class="btn btn-secondary" type="submit">Delete</button>
</form>
</div>
</div>
Upvotes: 1
Reputation: 3815
You have to implement it inside a form with a POST verb, as so
<form action="/logout" method="post">
{{csrf_field()}}
<a href='#' onclick='this.parentNode.submit(); return false;' class="nav-link">
<i class="nav-icon fa fa-sign-out-alt"></i>
<p>Log out</p>
</a>
</form>
Upvotes: 0