如何用Laravel替换$ _GET。我试图访问此href参数。我知道在标准PHP中你执行$ _GET请求后跟SQL删除查询语句。在Laravel,它更加困难,因为它涉及控制器/路线。我在ProjectsController中有一个destroy路由。以下链接是我将鼠标悬停在链接上时显示的内容。我得到了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);
}
}
答案 0 :(得分:2)
试试这个:
Route::get('projects/delete/{id}', 'ProjectsController@destroy')->where('id', '[0-9]+');
并更新你的href:
<a href="projects/delete/31"> delete</a>
它会像这样工作。您也可以删除该行:$request->input('delete');
答案 1 :(得分:2)
更改
<a href="projects/delete=31">delete</a> // this is wrong
到
Approch 01
<a href="projects?delete=31">delete</a>
将路线设为
Route::get('projects', 'ProjectsController@destroy');
将ProjectsController设置为
class ProjectsController
{
public function destroy()
{
$delete = request()->get('delete');
}
}
Approch 02
<a href="projects/delete/31">delete</a>
将路线设为
Route::get('projects/{id}', 'ProjectsController@destroy');
将ProjectsController设置为
class ProjectsController
{
public function destroy($id) // $id = 31
{
// your code
}
}
请参阅routing和controllers以掌握它们。
答案 2 :(得分:0)
将链接更改为:
<a href="/projects?delete=31"> delete</a>
确保路线如下:
Route::get('projects', ....
然后,您可以在控制器,模型等中使用request('delete')
访问此参数。
答案 3 :(得分:0)
也许你可以这样试试
<a href="{{ asset('projects/delete/{id}') }}">delete</a>
并设置路线
Route::get('/projects/delete/{id}', 'ProjectsController@delete');
关于ProjectsController中的删除功能
public function delete($id)
{
$projects = projects::find($id);
$projects->delete();
return redirect('/yourEditPage');
}
希望这可以帮到你!
答案 4 :(得分:0)
其他答案涵盖了如何通过路由访问get参数,这确实可以回答您的原始问题。
但是,我会建议您在删除内容时遵循restful模式(使用DELETE
方法)。在laravel中它非常简单。
路线
Route::delete('projects/{project}', 'ProjectsController@destroy')->name('projects.destroy');
控制器方法
public function destroy(Project $project) {
$project->delete();
Session::flash('success', 'The project was successfully deleted!');
return redirect()->route('projects.index');
}
模板
<form action="{{route('projects.destroy', ['project' => $project->id])}}" method="POST">
{{method_field('DELETE')}}
{{csrf_field()}}
<button type="submit">Delete</button>
</form>