Saturday, February 19, 2022

[FIXED] Delete Function: Laravel 5

Issue

new to laravel framework here. I'm having quite a problem here calling out the delete function in my resource controller. Seems like it is not deleting the selected id. Thanks for your help in advance.

resources/views/bufashaccts/allAccounts.blade.php

@extends('adminlte::page')
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<h1>view accounts!</h1>
@foreach($bfaccounts as $userAccount)
    <p>{{ $userAccount->acct_firstname }}</p><br>
    <p>{{ $userAccount->acct_middlename }}</p><br>
    <p>{{ $userAccount->acct_lastname }}</p>
    @if ($userAccount->id)
        <form action="/Accounts" method="POST">
            {{ csrf_field() }}
            {{ method_field('DELETE') }}
            <a href="/Accounts">
                <button type="button">delete</button>
            </a>
        </form>
    @endif
    <a href="/Accounts/{{ $userAccount->id }}/edit">
        <button type="button">edit</button>
    </a>
@endforeach
</body>
</html>

app/http/controllers/AccountsController.php

<?php

namespace App\Http\Controllers;

use App\bufashaccounts;
use Illuminate\Http\Request;

class AccountsController extends Controller
{

    public function index()
    {
        $bfaccounts = bufashaccounts::all();

        return view('bufashaccts.allAccounts', compact('bfaccounts'));
    }

    public function create()
    {
        return view('bufashaccts.addAccounts');
    }

    public function store(Request $request)
    {
        bufashaccounts::create($request->all());

        return "success!";
    }

    public function show($id)
    {
        $bfshowAccounts = bufashaccounts::findOrFail($id);

        return view('bufashaccts.viewAccounts', compact('bfshowAccounts'));
        //return $bfshowAccounts;
    }

    public function edit($id)
    {
        $bfeditAccounts = bufashaccounts::findOrFail($id);

        return view('bufashaccts.editAccounts', compact('bfeditAccounts'));
    }

    public function update(Request $request, $id)
    {
        $bfeditAccounts = bufashaccounts::find($id);
        $bfeditAccounts->update($request->all());

        return redirect('Accounts');
    }

    public function destroy($id)
    {
        //$bfdeleteAccounts = bufashaccounts::findOrFail($id);
        //$bfdeleteAccounts->delete();
        //return 'delete';
        $bfaccounts = bufashaccounts::findOrFail($id);
        $bfeditAccounts->delete();

        //return view('bufashaccts.allAccounts', compact('bfaccounts'));
        return redirect('/Accounts');
    }
}

Solution

You would need to change your form to be something like:

<form action="{{ url("/Accounts/$userAccount->id") }}" method="POST">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button type="submit">delete</button>
</form>

Hope this helps!



Answered By - Rwd

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.