简体   繁体   English

删除功能:Laravel 5

[英]Delete Function: Laravel 5

new to laravel framework here.这里是 Laravel 框架的新手。 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.似乎它没有删除选定的 ID。 Thanks for your help in advance.提前感谢您的帮助。

resources/views/bufashaccts/allAccounts.blade.php资源/视图/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 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');
    }
}

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!希望这可以帮助!

  1. Add this in your route file将此添加到您的路由文件中

    Route::delete('account/delete/{id}', ['as' => 'account.delete', 'uses' => 'AccountsController@destroy'])
  2. Modify your blade file修改您的刀片文件

    <form action="{{ route('account.delete') }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit" class="btn btn-danger">Delete</button>

Try this way.试试这个方法。 Hope it will work.希望它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM