Search code examples
phplaravellaravel-5.8maatwebsite-excellaravel-excel

How to add custom ini_set in Laravel


I want to export data from a view into CSV file at live server but whenever I run it I get this error:

Maximum execution time of 30 seconds exceeded

And here is the Export Class:

class StudentExportView implements FromView {
    /**
     * @return \Illuminate\Support\Collection
     */
    public function view(): View {
        $student = new Student();
        $students = $student->searchStudents()->paginate(10);
        $custom = new Student();
        $customs = $custom->all();
        return view('admin.students.custom', compact('students','customs'));
    }
}

And at the Controller:

public function export_view() {
    return Excel::download(new StudentExportView, 'studentlist.csv');
}

Actually, there is a large amount of data, that's why this error occurred. But they say if I add ini_set('max_execution_time', 300); I can get rid of this error.

So the question is where to add this custom ini_set? I mean at the Controller or Export Class?

Is there any solution to run this code on a live server?

I would really appreciate any idea or suggestion from you guys.

Thanks in advance.


Solution

  • Write at the beginning of the specific function at Controller.

       /**
         * @return \Illuminate\Support\Collection
         */
        public function view(): View
        {
            ini_set('max_execution_time', 0); // 0 = Unlimited
            $student = new Student();
            $students = $student->searchStudents()->paginate(10);
            $custom = new Student();
            $customs = $custom->all();
            return view('admin.students.custom', compact('students','customs'));
        }