Glen D P Maramis
Glen D P Maramis

Reputation: 1

Undefined Constant when trying to catch an Id in Laravel

I have a function in Laravel which running this code below

$data = AnggotaModel::where([
        ['status_nikah','Menikah'],
        ['jk','Pria']
    ])->get();

below is the screenshoot captured from dd($data);

result of dd($data);

What I want is to catch the id so I use this code

$id = $data[id];

as the result, it ends up with an Error

What should I do to fix this error??

Upvotes: 0

Views: 73

Answers (1)

Mahdi Jedari
Mahdi Jedari

Reputation: 758

The get() method gives you a collection of items. Get the first element then use $data[id]. Here you should change it to:

$id = $data->first()[id];

// Or in a better way:

$id = $data->first()->id;

Upvotes: 3

Related Questions