ivan setiadi
ivan setiadi

Reputation: 107

Show data from controller to view laravel

in there i want to show data from my controller to my view but i geting error. so now i want to ask how ? let me show my code.

this my controller :

public function getBulan($nama)
    {

        $data['users'] = DB::table('lembur_karyawan')
                ->select('nama', DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( total ) ) ) as   total_lembur'))
                ->groupBy('nama')
                ->havingRaw('SUM(total)')
                ->get();

        return view('bulan_user',$data);
    }

and this is my view :

<?php echo $users->{'total_lembur'} ?>

what i want other table if i use first();

have someone know how ? i have no idea how to do it .

Upvotes: 0

Views: 871

Answers (3)

linuxartisan
linuxartisan

Reputation: 2426

Do what @Md Mahfuzur Rahman has suggested.

Then in the view, you will have to run a foreach loop.

@foreach($users as $user)
    {{ $user->total_lembur }}
@endforeach

Upvotes: 0

Vinod VT
Vinod VT

Reputation: 7149

No need to use variable data.

Use like,

$users = DB::table('lembur_karyawan')
            ->select('nama', DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( total ) ) ) as   total_lembur'))
            ->groupBy('nama')
            ->havingRaw('SUM(total)')
            ->get();

return view('bulan_user', ['users' => $users]);   

Print it in your view like,

@foreach($users as $user)    

 {{ $user->total_lembur }}

@endforeach

Upvotes: 1

Md Mahfuzur Rahman
Md Mahfuzur Rahman

Reputation: 2359

You can use compactalso like this:

public function getBulan($nama)
{

    $users = DB::table('lembur_karyawan')
            ->select('nama', DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( total ) ) ) as   total_lembur'))
            ->groupBy('nama')
            ->havingRaw('SUM(total)')
            ->get();

    return view('bulan_user', compact('users'));
}

In the view:

{{ $user->total_lembur }}

Upvotes: 0

Related Questions