ErrorException compact(): Undefined variable: Sep โ€“ laravel 8

0๐Ÿ‘

โœ…

You can not pass single varibles with array of variables inside compact method. You can do two step here for passing each varible to view.

Method 1 => you can pass each variable as comma separated list inside compact and get in front end by variable name

return view('dashboard.admins', compact('user','jumlah_user','data','months','monthCount' ));

Method 2 => You can pass a single variable as array of different variables like

$view_data['user'][]=$user;
$view_data['jumlah_user'][]=$jumlah_user;
$view_data['months'][]=$month;
$view_data['data'][]=$data;

return view('dashboard.admins', compact('view_data'));

And use as normal on front end by accessing variable name

{{ $user }} {{ $months }} {{ $jumlah_user }}

๐Ÿ‘:1

Here is what you should do. Do not use compact as it cannot have an array as an argument. Instead you should pass all your data as an array which is broken down into the various variables when accessing it in your view file.

Adjust your code to this:

if(Auth::user()->hasRole('Super Admin')) {
            $view_data['user'] = User::where('id', Auth::user()->id)->first();
            $view_data['jumlah_user'] = User::orderBy('id', 'ASC')->get()->count();

            $view_data['data'] = Verifikasi_tte::join('surat_ttes', 'surat_ttes.id', '=', 'verifikasi_ttes.surat_tte_id')
                ->join('users', 'users.id', '=', 'surat_ttes.user_id')
                ->join('jenis_surat_ttes', 'jenis_surat_ttes.id', '=', 'surat_ttes.jenis_surat_tte_id')->select('surat_ttes.id','surat_ttes.klasifikasi','surat_ttes.no_surat','surat_ttes.tgl_surat','surat_ttes.created_at',
                'jenis_surat_ttes.nama_jenis_surat','users.name','users.opd_id','verifikasi_ttes.status')
                ->get()->groupBy(function($data){
                return Carbon::parse($data->created_at)->format('M');
            });
    
            $view_data['months']=[];
            $view_data['monthCount']=[];
            foreach($view_data['data'] as $month => $values){
                $view_data['months'][]=$month;
                $view_data['monthCount'][]=count($values);
            }

            return view('dashboard.admins',  $view_data);
        }

Now in your view file. You can just reference to each variable as you wish like this:
{{ $user }} {{ $months }} {{ $jumlah_user }} etcetera

Leave a comment