Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

salm's avatar
Level 1

Problem when trying to export a list of users

I don't have much knowledge in Laravel When I try to export the list of students in a specific course, I get an error "Too few arguments to function". My questions is: What is the cause of the problem and why does it appear that 3 Arguments should be passed instead of 2 . I would appreciate any help.

public function studentsLists(Request $request, $id) { $this->authorize('admin_webinar_students_lists');

    $webinar = Webinar::where('id', $id)
        ->with([
            'teacher' => function ($qu) {
                $qu->select('id', 'full_name');
            },
            'chapters' => function ($query) {
                $query->where('status', 'active');
            },
            'sessions' => function ($query) {
                $query->where('status', 'active');
            },
            'assignments' => function ($query) {
                $query->where('status', 'active');
            },
            'quizzes' => function ($query) {
                $query->where('status', 'active');
            },
            'files' => function ($query) {
                $query->where('status', 'active');
            },
        ])
        ->first();


    if (!empty($webinar)) {

        $query = User::join('sales', 'sales.buyer_id', 'users.id')
            ->leftJoin('webinar_reviews', function ($query) use ($webinar) {
                $query->on('webinar_reviews.creator_id', 'users.id')
                    ->where('webinar_reviews.webinar_id', $webinar->id);
            })
            ->select('users.*', 'webinar_reviews.rates', 'sales.access_to_purchased_item', 'sales.id as sale_id', DB::raw('sales.created_at as purchase_date'))
            ->where('sales.webinar_id', $webinar->id)
            ->whereNull('sales.refund_at');

        $students = $this->studentsListsFilters($webinar, $query, $request)
            ->orderBy('sales.created_at', 'desc')
            ->paginate(10);

        $userGroups = Group::where('status', 'active')
            ->orderBy('created_at', 'desc')
            ->get();

        $totalExpireStudents = 0;
        if (!empty($webinar->access_days)) {
            $accessTimestamp = $webinar->access_days * 24 * 60 * 60;

            $totalExpireStudents = User::join('sales', 'sales.buyer_id', 'users.id')
                ->select('users.*', DB::raw('sales.created_at as purchase_date'))
                ->where('sales.webinar_id', $webinar->id)
                ->whereRaw('sales.created_at + ? < ?', [$accessTimestamp, time()])
                ->whereNull('sales.refund_at')
                ->count();
        }

        $webinarStatisticController = new WebinarStatisticController();

        $allStudentsIds = User::join('sales', 'sales.buyer_id', 'users.id')
            ->select('users.*', DB::raw('sales.created_at as purchase_date'))
            ->where('sales.webinar_id', $webinar->id)
            ->whereNull('sales.refund_at')
            ->pluck('id')
            ->toArray();

        $learningPercents = [];
        foreach ($allStudentsIds as $studentsId) {
            $learningPercents[$studentsId] = $webinarStatisticController->getCourseProgressForStudent($webinar, $studentsId);
        }

        foreach ($students as $student) {
            $student->learning = !empty($learningPercents[$student->id]) ? $learningPercents[$student->id] : 0;
        }

        $roles = Role::all();

        $data = [
            'pageTitle' => trans('admin/main.students'),
            'webinar' => $webinar,
            'students' => $students,
            'userGroups' => $userGroups,
            'roles' => $roles,
            'totalStudents' => $students->total(),
            'totalActiveStudents' => $students->total() - $totalExpireStudents,
            'totalExpireStudents' => $totalExpireStudents,
            'averageLearning' => count($learningPercents) ? round(array_sum($learningPercents) / count($learningPercents), 2) : 0,
        ];

        return view('admin.webinars.students', $data);
    }

    abort(404);
}

private function studentsListsFilters($webinar, $query, $request)
{
    $from = $request->input('from');
    $to = $request->input('to');
    $full_name = $request->get('full_name');
    $sort = $request->get('sort');
    $group_id = $request->get('group_id');
    $role_id = $request->get('role_id');
    $status = $request->get('status');

    $query = fromAndToDateFilter($from, $to, $query, 'sales.created_at');

    if (!empty($full_name)) {
        $query->where('users.full_name', 'like', "%$full_name%");
    }

    if (!empty($sort)) {
        if ($sort == 'rate_asc') {
            $query->orderBy('webinar_reviews.rates', 'asc');
        }

        if ($sort == 'rate_desc') {
            $query->orderBy('webinar_reviews.rates', 'desc');
        }
    }

    if (!empty($group_id)) {
        $userIds = GroupUser::where('group_id', $group_id)->pluck('user_id')->toArray();

        $query->whereIn('users.id', $userIds);
    }

    if (!empty($role_id)) {
        $query->where('users.role_id', $role_id);
    }

    if (!empty($status)) {
        if ($status == 'expire' and !empty($webinar->access_days)) {
            $accessTimestamp = $webinar->access_days * 24 * 60 * 60;

            $query->whereRaw('sales.created_at + ? < ?', [$accessTimestamp, time()]);
        }
    }

    return $query;
}

 public function exportCourseExcel(Request $request)
{
    $this->authorize('admin_webinars_export_excel');

    $query = Webinar::query();

    $query = $this->studentsListsFilters($query, $request);

    $webinars = $query->get();

    $webinarExport = new CourseStudentsExport($webinars);

    return Excel::download($webinarExport, 'course.xlsx');
}
0 likes
7 replies
Sinnbeck's avatar

Please share the full error. It should say which exact method in what class is failing

salm's avatar
Level 1

Thank you

ArgumentCountError

Too few arguments to function App\Http\Controllers\Admin\WebinarController::studentsListsFilters(), 2 passed in /home/trainingtarbeyac/public_html/app/Http/Controllers/Admin/WebinarController.php on line 891 and exactly 3 expected

Sinnbeck's avatar

@salm ok so this line. Only 2 arguments are passed

$query = $this->studentsListsFilters($query, $request); 
Sinnbeck's avatar

@salm well the method expects a $webinar but are not passing one? So pass one as the first argument

salm's avatar
Level 1

@Sinnbeck Thank you.... Unfortunately I did a lot of experiments and it didn't work The problem is that I don't know what to pass in $query , I also added a parameter with the course number $id I will be happy for any help

public function exportCourseExcel(Request $request, $id)
{
    $this->authorize('admin_webinars_export_excel');

    //$query = Webinar::query()->where('id', $id);
    
     $webinar = Webinar::query()->where('id', $id);

    $query = $this->studentsListsFilters($webinar,$id, $request);

    $webinars = $query->get();

    $webinarExport = new CourseStudentsExport($webinars);
    
    return Excel::download($webinarExport, 'course.xlsx');
}
Sinnbeck's avatar

@salm I didn't write it so I don't know what the overall idea is. Maybe ask whoever wrote the method?

Please or to participate in this conversation.