php “类型错误:函数 App\Http\Controllers\UserController::attendance() 的参数太少,0 已通过,预期为 1”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47726929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected"
提问by Alexey Mezenin
I have two table in my database which are user and the attendance table. What I want to do now is I want to show the attendance data from database according to the user in their attendance view which is linked with their profile. This is my attendance function in the userController.
我的数据库中有两个表,分别是用户表和出勤表。我现在想要做的是我想根据用户在与他们的个人资料链接的考勤视图中显示数据库中的考勤数据。这是我在 userController 中的考勤功能。
public function attendance($id)
{
$user = UserProfile::findOrFail($id);
$this->authorize('modifyUser', $user);
return view ('user.attendance', ['user'=>$user]);
}
This is my route to attendance view.
这是我查看出勤率的途径。
Route::get('/attendance/', ['as' => 'user.attendance', 'uses' => 'UserController@attendance']);
This is my attendance view.
这是我的出席观。
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><i class="fa fa-university"></i> Attendance</h1>
</div>
<div class="row">
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Date</th>
<th>Time</th>
<th>Present</th>
</tr>
<?php $no=1; ?>
<tr>
<td>{{ $no++ }}</td>
<td>{{$user->attendance->date}}</td>
<td>{{$user->attendance->time}}</td>
<td>{{$user->attendance->present}}</td>
</tr>
</table>
</div>
</div>
</div>
@stop
This is the error that i got.Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected". I am new to laravel.
这是我得到的错误。类型错误:函数 App\Http\Controllers\UserController::attendance() 的参数太少,0 已通过,预期为 1”。 我是 laravel 的新手。
回答by Alexey Mezenin
You're getting this error because attendance()
method expects an ID and you don't pass it. Change the route to:
您收到此错误是因为attendance()
方法需要一个 ID 而您没有传递它。将路线改为:
Route::get('attendance/{id}', ['as' => 'user.attendance', 'uses' => 'UserController@attendance']);
And pass an ID when creating a link to the attendance()
method:
并在创建指向该attendance()
方法的链接时传递一个 ID :
{{ route('user.attendance', ['id' => 1]) }}
Or:
或者:
{{ url('attendance/1') }}
If you want to get ID of currently logged in user, do not pass ID. Use auth()-user()
instead:
如果您想获取当前登录用户的 ID,请不要传递 ID。使用auth()-user()
来代替:
public function attendance()
{
$this->authorize('modifyUser', auth()->user());
return view ('user.attendance');
}