1

i want to echo ouptut from controller When i dump this object i get all data like i want, but how to echo that I want to display this data like this

Here is my controller

$pkm = DB::table('magacin_pkms')->select(
    'magacin_pkms.status',
    'magacin_pkms.izdao_to',
    'users.name as izdaoFirstName',
    'users.lastname as izdaoLastName',
    'magacin_pkms.vrednost',
    'magacin_pkms.izdao_by',
    'magacin_pkms.izdao_at'
)
    ->join('users', 'id_user', 'izdao_to')
    ->where('magacin_pkms.status', 2)
    ->orderBy('izdao_to')
    ->get();

$driverIDs = array_unique($pkm->pluck('izdao_to')->toArray());

foreach ($driverIDs as $id) {
    $obj  = new StdClass;
    $name = $pkm->where('izdao_to', $id)->first()->izdaoFirstName;
    $lastname = $pkm->where('izdao_to', $id)->first()->izdaoLastName;
    $cnt = $pkm->where('izdao_to', $id)->count();
    $total = $pkm->where('izdao_to', $id)->sum('vrednost');
    $admin = $pkm->where('izdao_to', $id)->first()->izdao_by;
    $izdato = $pkm->where('izdao_to', $id)->first()->izdao_at;
  
    $obj->name = $name;
    $obj->lastname = $lastname;
    $obj->cnt = $cnt;
    $obj->total = $total;
    $obj->admin = $admin;
    $obj->izdato = $izdato;
    dump($obj);
}

this is my view

@foreach ($obj as $items)
    <tr>
        <td>{{$loop->iteration}}</td>
        <td>{{$obj->name}}</td>
        <td>{{$obj->lastname}}</td>
        <td>{{$obj->cnt }}</td>
        <td>{{$obj->total}}</td>
        <td>{{\App\User::find($obj->admin)->name}}</td>
        <td>{{$obj->izdato }}</td>
    </tr>
@endforeach

i think i need one more foreach loop in controller where is $obj->name = $name

4
  • In your controller, what variable does you return? Commented Dec 22, 2020 at 13:45
  • i tried to return all, but its the same, for now i return obj Commented Dec 22, 2020 at 13:48
  • Could you please share the code which return obj in your controller? Commented Dec 22, 2020 at 13:54
  • 1
    do you think on this ? ` return view('superadmin/izvestaji/izvestajiVozaca', compact('pkm','obj')); ` Commented Dec 22, 2020 at 14:05

2 Answers 2

2

You just create an array which contains all objects and return this array in your controller.

$pkm = DB::table('magacin_pkms')->select(
    'magacin_pkms.status',
    'magacin_pkms.izdao_to',
    'users.name as izdaoFirstName',
    'users.lastname as izdaoLastName',
    'magacin_pkms.vrednost',
    'magacin_pkms.izdao_by',
    'magacin_pkms.izdao_at'
)
    ->join('users', 'id_user', 'izdao_to')
    ->where('magacin_pkms.status', 2)
    ->orderBy('izdao_to')
    ->get();

$driverIDs = array_unique($pkm->pluck('izdao_to')->toArray());

//you initialize an array 
$arrObj = [];
foreach ($driverIDs as $id) {
    $obj  = new StdClass;
    $name = $pkm->where('izdao_to', $id)->first()->izdaoFirstName;
    $lastname = $pkm->where('izdao_to', $id)->first()->izdaoLastName;
    $cnt = $pkm->where('izdao_to', $id)->count();
    $total = $pkm->where('izdao_to', $id)->sum('vrednost');
    $admin = $pkm->where('izdao_to', $id)->first()->izdao_by;
    $izdato = $pkm->where('izdao_to', $id)->first()->izdao_at;
  
    $obj->name = $name;
    $obj->lastname = $lastname;
    $obj->cnt = $cnt;
    $obj->total = $total;
    $obj->admin = $admin;
    $obj->izdato = $izdato;
    dump($obj);
    //you add this object into array
    $arrObj[] = $obj;
}

return view('superadmin/izvestaji/izvestajiVozaca', compact('arrObj'));

In your view

@foreach ($arrObj as $obj)
    <tr>
        <td>{{$loop->iteration}}</td>
        <td>{{$obj->name}}</td>
        <td>{{$obj->lastname}}</td>
        <td>{{$obj->cnt }}</td>
        <td>{{$obj->total}}</td>
        <td>{{\App\User::find($obj->admin)->name}}</td>
        <td>{{$obj->izdato }}</td>
    </tr>
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

0

You can add a dd in the view, like:

  • {{ dd($obj) }}
  • {{ dd($items) }} (inside foreach)

3 Comments

i'm getting results the same like i'm getting with dump
dd stands for dump and die, and dd will stop script execution, you'd never get more output than just the first one in the loop laravel.com/docs/8.x/helpers#method-dd
i know that, when i remove dump, and using dd i'm getting only one record, how to get all records, i think i need one more foreach in controller but i'm not so sure, i'm not so good in oop php

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.