Hamza Ghaissi
Hamza Ghaissi

Reputation: 100

how to read data from a collection in laravel

Hi im new to laravel and i have this collection

$details = collect([
            ['total' => Valeur::all()->count()],
            ['instance' => Valeur::where('etat', 1)->count()],
            ['deposer' => Valeur::where('etat', 2)->count()],
            ['encaisse' => Valeur::where('etat', 3)->count()],
            ['retourne' => Valeur::where('etat', 4)->count()],
            ]);

i was wondering how to read data like this

$details->total;

Upvotes: 0

Views: 35

Answers (1)

nakov
nakov

Reputation: 14268

You have array of arrays then you will need to iterate over it in order to access it as you want. But to me it looks like this should be one item of an array, so if you change it like this:

$details = collect([
    'total' => Valeur::all()->count(),
    'instance' => Valeur::where('etat', 1)->count(),
    'deposer' => Valeur::where('etat', 2)->count(),
    'encaisse' => Valeur::where('etat', 3)->count(),
    'retourne' => Valeur::where('etat', 4)->count(),
]);

Then you will be able to access it like $details->get('total'); or $details['total'].

Upvotes: 2

Related Questions