【问题标题】:Delete data without using form in Laravel在 Laravel 中不使用表单删除数据
【发布时间】:2018-05-18 17:52:09
【问题描述】:

这是我的按钮组,包含查看、编辑和删除

<div class="btn-group">
    <a href="" >View</a>
    <a href="" >Edit</a>
    <a href="{{ action('PhaseController@destroy', $phase->phase_id) }}" >Delete</a>
</div>

点击Delete按钮后,我已经定义了动作,该按钮将转到PhaseController中的destroy函数,并带有我要删除的阶段ID。`

相位控制器

<?php

namespace App\Http\Controllers\Developer_user;

use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Project;
use App\Model\Phase;

class PhaseController extends Controller
{

    public function show($id)
    {
        //After I click the Delete button, It always go to here.
        //How to go to destroy function ?

    }

    public function destroy($id)
    {    
        $deleted_phase = Phase::find($id);
        $deleted_phase->delete();

    }
}

但是,当我点击删除按钮后,它总是去显示功能,但没有去action('PhaseController@destroy')
我可以使用表单并将表单方法设置为 Delete 来解决这个问题。
但是有没有在不使用 Form 的情况下删除它?

更新
路由.php

【问题讨论】:

  • 你也可以分享路由器定义吗?
  • 当然,因为我使用的是资源路由。所以,我为你截取了我的路线列表。我已经突出显示了。

标签: php controller


【解决方案1】:

在这种情况下,您需要添加自定义路由。将以下行添加到 routes/web.php

Route::get('phase/destroy/{phaseId}', 'PhaseController@destroy');

要删除的网址 - http://your-domain.com/phase/destroy/1

<div class="btn-group">
    <a href="" >View</a>
    <a href="" >Edit</a>
    <a href="{{URL::to('/phase/destroy/'.$phase->phase_id)}}" >Delete</a>
</div>

【讨论】:

  • 感谢您的回复先生!
猜你喜欢
  • 2017-02-14
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 2017-06-02
  • 2016-09-04
相关资源
最近更新 更多