Laravel 5.2 删除功能给了我 NotFoundHttpException

Posted

技术标签:

【中文标题】Laravel 5.2 删除功能给了我 NotFoundHttpException【英文标题】:Laravel 5.2 delete function gave me NotFoundHttpException 【发布时间】:2017-09-05 18:45:52 【问题描述】:

我正在使用 Laravel 5.2 并尝试添加和删除我已经输入的数据,但是当我单击“删除”按钮时,它给了我 NotFoundHttpException。

这是我在控制器中删除功能代码的示例:

    <?php
namespace App\Http\Controllers\Track;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
//use Illuminate\Support\Facades\Input;
use Validator;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Track as Track;

class TrackController extends Controller

    /*Display track registry*/
    public function index()
    
        $data = array('track' => Track::all());
        return view('admin.dashboard.tracks.track',$data);
    

    /*Display create track form*/
    public function create()
    
        return view('admin.dashboard.tracks.createtrack');
    

    /*Save data form*/
    public function saveTrack(Request $request)
    
        $input = $request->all();
        $messages = array(
            'trackCode.required'=>'Track code required.',
            'trackCode.unique'=>'Track code already exist.',
            'trackName.required'=>'Track name required.',
        );
        $rule = array(
            'trackCode' => 'required|unique:track',
            'trackName' => 'required|max:60',
        );

        $validator = Validator::make($input, $rule, $messages);

        if($validator->fails()) 
            #Directed to the same page with error message
            return Redirect::back()->withErrors($validator)->withInput();

            #Validate Success
        

        $track = new Track;
        $track->trackCode = $request['trackCode'];
        $track->trackName = $request['trackName'];

        if (! $track->save())
            App::abort(500);

        return Redirect::action('Track\TrackController@index')->with('successMessage','Track data "'.$input['trackName'].'" has been inserted.');
    

    /*Delete data*/
    public function delete($id)
    
        echo $id;
        /*$trackCode = Track::where('trackCode','=',$id)->first();

        if($trackCode==null)
          App::abort(404);
        $trackCode->delete();

        return Redirect::action('track');*/
    

这是我的删除选项代码的一部分:

<div class="box-body">
          <table class="table table-bordered table-striped">
            <thead>
            <tr>
              <th style="width: 150px; text-align: center;">Track Code</th>
              <th>Track Name</th>
              <th>Action</th>
            </tr>
            </thead>
            <tbody>
            @foreach($track as $itemTrack)
            <tr id="track-list" name="track-list">
              <td style="text-align: center;"> $itemTrack->trackCode </td>
              <td> $itemTrack->trackName </td>
              <td><a href=" action('Track\TrackController@delete',[$itemTrack->trackCode]) " title="Delete" onclick="return confirm('Are you sure you want to delete this track : $itemTrack->trackCode.' - '.$itemTrack->trackName ?')">
                    <span class="label label-danger"><i class="fa fa-trash"> Delete </i></span>
                  </a>                          
              </td>
            </tr>
            @endforeach
            </tbody>
          </table>
          <br/>
          <a href=" action('Track\TrackController@create') "><button class="btn btn-success pull-right" type="submit">Add Data</button></a>
        </div>
        <!-- /.box-body -->
      </div>
      <!-- /.box -->

每当它出现数据并且我尝试删除它时,它会转到一个页面并且出现 NotFoundHttpException 错误,而不是向我显示数据的 $id。

有人可以帮忙解释一下吗?谢谢

-编辑部分-

路线:

    <?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () 
    return view('welcome');
);

Route::group(['middleware' => 'web'], function()

    Route::auth();
);

//Route as admin
Route::group(['middleware' => ['web','role:admin']], function()

    Route::get('/users/dashboard', 'UserController@index');

    /*-----------------------------------------------Track Part---------------------------------------------------------*/

    /*Track index*/
    Route::get('/users/programs/track', array('as'=>'track', 'uses'=>'Track\TrackController@index'));

    /*Create track form*/
    Route::get('/users/programs/track/create', array('as'=>'track.create', 'uses'=>'Track\TrackController@create'));

    /*Route to save track*/
    Route::post('/users/programs/track/save', array('as'=>'track.save', 'uses'=>'Track\TrackController@saveTrack'));

    /*Delete track*/
    Route::get('/users/programs/track/$id/delete', array('as'=>'track.delete', 'uses'=>'Track\TrackController@delete'));

    /*-----------------------------------------------Course Part---------------------------------------------------------*/

    //Display course menu
    Route::get('/users/programs/course', array('as'=>'course', 'uses'=>'Course\CourseController@index'));

    //Delete course data
    Route::get('/users/programs/course/$id/delete', array('as'=>'course.delete', 'uses'=>'Course\CourseController@delete'));

    //Create course data
    Route::post('/users/programs/course/create', array('as'=>'course.create', 'uses'=>'Course\CourseController@createCourse'));

    //Edit course data
    Route::get('/users/programs/course/$id/edit', array('as'=>'course.edit', 'uses'=>'Course\CourseController@editCourse'));

    //Save editted course data
    Route::put('/users/programs/course/$id/saveEdit', array('as'=>'course.saveEdit', 'uses'=>'Course\CourseController@saveEdit'));
);

【问题讨论】:

你能显示你的路线吗? 【参考方案1】:

用这个替换删除锚标记:-

<a href=" url('users/programs/track/'.$itemTrack->trackCode.'/delete') " title="Delete" onclick="return confirm('Are you sure you want to delete this track : $itemTrack->trackCode.' - '.$itemTrack->trackName ?')">

并更改删除路线:-

Route::match(['get', 'post'], '/users/programs/track/id?/delete', 'Track\@TrackController@delete');

希望对你有帮助!

【讨论】:

【参考方案2】:

来自文档:

"如果方法接受路由参数,你可以将它们作为第二个参数传递给方法:

$url = action('UserController@profile', ['id' => 1]);"

我猜“id”的定义不正确。试试这个:

action('Track\TrackController@delete',['id' => $itemTrack->trackCode])

【讨论】:

【参考方案3】:

试试这个:

!! action('track.delete',[$itemTrack->trackCode]) !!

【讨论】:

【参考方案4】:

我猜您在路由中使用了DELETE,这不适用于a 链接。您需要创建一个表单并欺骗DELETE。你可以在here找到更多关于这样做的信息。

或者,您可以将Route::delete() 更改为Route::get(),但不建议这样做。

【讨论】:

我正在使用 Route::get() 之一,但我有点跳过或不可读的 TrackController 的删除功能【参考方案5】:

我认为这可能与您的三重括号有关。请改用 action('Track\TrackController@create')

更多信息请阅读link

【讨论】:

还是一样,仍然将我重定向到 NotFoundHttpException。

以上是关于Laravel 5.2 删除功能给了我 NotFoundHttpException的主要内容,如果未能解决你的问题,请参考以下文章

在 forge 上安装 Laravel 5.2 时出错

Laravel 5.2:无法调用构造函数

Laravel 5.1 PHPUnit - press() 给了我“无法访问的字段”“”

Laravel 5.2 重置密码 + Mandrill

我在 laravel 5.6 路线中更改了 web.php,但仍然给了我旧路线

Laravel 5.2 删除外键