嘿我正在编写一个代码,当用户点击更新按钮时,它将转到另一个是doctorEdit的页面。我在我的网络文件中定义了路由,但它一次又一次地给出了未定义的错误路由。任何人都可以帮我解决我的问题。以下是我的代码。
路线代码:
Route::resource('doctor/doctorEdit','DoctorController@edit');
控制器代码
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class DoctorController extends Controller
{
public function edit()
{
return view('doctor.doctorEdit');
}
}
我的观看代码是
<form class="row" method="POST" action="#" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor/doctorEdit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
请让我知道我做错了什么因为我是laravel的新人。
答案 0 :(得分:4)
尝试将GET方法与命名路由一起使用。
您的路线文件
Route::get('doctor/doctorEdit','DoctorController@edit')->name('doctor.edit');
您的观看代码
<form class="row" method="POST" action="#" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor.edit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
答案 1 :(得分:1)
您做错了,请查看Resource Controllers上的文档。
基本上,如果使用Route::resource()
方法定义资源,则不能指定控制器操作,因为资源控制器应该提供兼容的REST方法。
因此,如果要创建REST资源控制器,则必须将路由指定为:
Route::resource('doctor', 'DoctorController')
;
所以你必须只指定你的控制器类名。
然后在您的控制器中,您必须指定所需的方法:
class DoctorController extends Controller
{
public function index()
{
// GET yourapp.com/doctor -> typically return all doctors
}
public function create()
{
// GET yourapp.com/doctor/create -> typically show doctor creation form
}
public function show()
{
// GET yourapp.com/doctor/{doctor_id} -> show a single doctor
}
public function store()
{
// POST yourapp.com/doctor -> create a new doctor
}
public function edit()
{
// GET yourapp.com/doctor/{doctor_id}/edit -> show edit form view
return view('doctor.doctorEdit');
}
public function update()
{
// PUT|PATCH yourapp.com/doctor/{doctor_id} -> update a doctor
}
public function destroy()
{
// DELETE yourapp.com/doctor/{doctor_id} -> delete a doctor
}
}
如果您只想在没有REST逻辑的情况下公开edit
表单,请使用Request::get()
方法:
Request::get('doctor/doctorEdit', 'DoctorController@edit')->name('doctor.edit');
如果您要缓存路线,请记住使用artisan命令序列刷新它们
php artisan route:clear
php artisan route:cache
或更简洁
php artisan route:cache
答案 2 :(得分:0)
您正在定义资源。正确的方法是:
Route::resource('doctor','DoctorController');
DELETE方法的名称为doctor.destroy
,控制器方法为destroy
您的观看代码应为
<form class="row" method="POST" action="{{ route('doctor.delete') }}" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor.edit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>