提问者:小点点

访问laravel中的href参数


如何将$\u GET替换为Laravel。我正在尝试访问此href参数。我知道在标准PHP中,您执行$\u GET请求,然后执行SQL delete查询语句。在Laravel中,这更为困难,因为它涉及控制器/路由。我在ProjectsController中有一条破坏路线。下面的链接是我将鼠标悬停在链接上时显示的内容。我用Jquery获得了id并传递了它,但问题是每当单击此链接时,我希望它从数据库中删除项目。

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

控制器:

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

    }

}

共3个答案

匿名用户

试试这个:

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

并更新您的href:

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

它将像这样工作。您还可以删除该行:$request-

匿名用户

改变

<a href="projects/delete=31">delete</a> // this is wrong

方法01

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

将您的路线设置为

Route::get('projects', 'ProjectsController@destroy');

将项目控制器设置为

class ProjectsController
{
    public function destroy()
    {
        $delete = request()->get('delete');
    }
}

方法02

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

将您的路线设置为

Route::get('projects/{id}', 'ProjectsController@destroy');

将项目控制器设置为

class ProjectsController
{
    public function destroy($id) // $id = 31
    {
        // your code
    }
}

请参考路由和控制器以了解它们。

匿名用户

将链接更改为:

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

确保路线如下所示:

Route::get('projects', ....

然后在中,您可以通过控制器、模型等中的request('delete')访问此参数。