【发布时间】:2020-12-09 10:54:57
【问题描述】:
我正在制作一个应用程序,我试图根据角色显示不同的元素,但我在标题中收到了该错误。
- 在 AppointmentController.php 中:
class AppointmentController extends Controller
{
public function indexAll()
{
return Appointment::all();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($user)
{
$appointments = Appointment::where('user_id', '=', $user)->get();
return $appointments;
}
}
- Api.php:
Route::post('appointment/show', [AppointmentController::class,'index'])->middleware('auth:api'); //Role Client.
Route::get('appointment/showAll',[AppointmentController::class,'indexAll'])->middleware('role:admin'); //Role Admin.
【问题讨论】:
-
index()方法需要参数并且您没有发送它。 -
您必须键入提示
$user,否则 DI 不知道要为您插入什么。所以应该是index(User $user) -
尝试使用这个 Route::post('appointment/show/{$user}', [AppointmentController::class,'index'])->middleware('auth:api'); //角色客户端。
-
您的路由在定义中应该有一个路由参数,以便在控制器方法
Route::post('appointment/show/{user}', [AppointmentController::class,'index'])->middleware('auth:api');中可用在您查看发出请求的位置时,您必须传递用户的 ID(如果角色是客户端)作为路由参数
标签: php laravel function routes