7

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.

 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.

1
  • what's your requirement? i am not able tounderstand
    – kunal
    Commented Dec 9, 2017 at 9:45

1 Answer 1

12

You're getting this error because attendance() method expects an ID and you don't pass it. Change the route to:

Route::get('attendance/{id}', ['as' => 'user.attendance', 'uses' => 'UserController@attendance']);

And pass an ID when creating a link to the attendance() method:

{{ 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:

public function attendance()
{
    $this->authorize('modifyUser', auth()->user());

    return view ('user.attendance');
}
4
  • if i logged in with user id two, it can show user id 2's attendance ? link to attendance view i did like this >> ' <input type="hidden" name="_token" value="{{ csrf_token() }}"> <a href="{{route('user.attendance', $user->id)}}", class="btn btn-primary"><i class="fa fa-university pull-left"> Attendance</i></a>'
    – user8062999
    Commented Dec 9, 2017 at 8:58
  • 1
    @Htoo look at the updated answer. You don't need to pass currently authenticated user ID to the method or to the view. Just use auth()->user() to get current user instance and auth()->id() to get his ID. Commented Dec 9, 2017 at 9:01
  • Mezein, I already edit like you showed me it seems like it works but when i go to attendance view by clicking the button. in the attendance view, it shows, you don't have permission.
    – user8062999
    Commented Dec 9, 2017 at 9:05
  • 1
    @Htoo that's not related to the issue you've described in your question. It's related to the modifyUser policy. So, you need to fix the policy. If you will not be able to do that by yourself, please create another question and post the policy and related code there. Commented Dec 9, 2017 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.