Wednesday, April 12, 2017

Laravel 5.2 delete function gave me NotFoundHttpException

I'm using Laravel 5.2 and trying to do an add and delete a data that I already Inputted but when i clicked "Delete" button it gave me NotFoundHttpException.

Here's example of my delete function code in controller:

    <?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');*/
    }
}

and here's the part of my delete option code:

<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;"></td>
              <td></td>
              <td><a href="}" title="Delete" onclick="return confirm('Are you sure you want to delete this track : }?')">
                    <span class="label label-danger"><i class="fa fa-trash"> Delete </i></span>
                  </a>                          
              </td>
            </tr>
            @endforeach
            </tbody>
          </table>
          <br/>
          <a href="}"><button class="btn btn-success pull-right" type="submit">Add Data</button></a>
        </div>
        <!-- /.box-body -->
      </div>
      <!-- /.box -->

whenever it appears the data and i try to delete it, it went to a page and there's NotFoundHttpException error instead of showing me the $id of the data.

Can someone help and explain? thanks

-Edited part-

Routes:

    <?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'));
});



via Hans Adrianus

Advertisement