Monday, April 10, 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



via Hans Adrianus

Advertisement