3

The delete function Laravel 5.2 gave me NotFoundHttpException

 2 years ago
source link: https://www.codesd.com/item/the-delete-function-laravel-5-2-gave-me-notfoundhttpexception.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The delete function Laravel 5.2 gave me NotFoundHttpException

advertisements

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;">{{ $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 -->

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


I'm guessing that you are using DELETE in your route, which wouldn't work with an a link. You'd need to create a form and spoof the DELETE. You can find more about doing it here.

Alternatively, you can change Route::delete() to Route::get(), but this isn't recommended.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK