Search code examples
phplaravelparametershref

Accessing href parameters in laravel


How to replace $_GET with Laravel's. I'm trying to access this href parameter. I know in standard PHP you execute $_GET request followed by SQL delete query statement. In Laravel it's more difficult because it involves controllers/routes. I have a destroy route in the ProjectsController. The below link is what is displayed when i hover over the link. I got the id with Jquery and passed it through, but the problem is I want it to delete the project from database whenever this link is clicked.

<a href="projects/delete=31"> delete</a>

Controller:

<?php

namespace App\Http\Controllers;

use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;

class ProjectsController extends Controller
{



    public function destroy($id){


        $project = Project::find($id);

        $request->input('delete'); 

        $project->delete();

        Session::flash('success', 'The project was successfully deleted!');

        return redirect()->route('projects.show', $project->project_id);

    }

}

Solution

  • Try this:

    Route::get('projects/delete/{id}', 'ProjectsController@destroy')->where('id', '[0-9]+');
    

    and update your href:

    <a href="projects/delete/31"> delete</a>
    

    It will work like this. Aslo you can remove that line: $request->input('delete');