anlogg
anlogg

Reputation: 1090

Cannot access protected property

I'm learning Laravel's(4) Eloquent and I'm lost when it comes to accessing properties from the returned Eloquent object. This code doesn't work:

public function show($email)
{
    $client = Client::where('email', $email)->get();
    echo var_dump($client->items);

}

The items property, which contains all the sub properties including email, is protected which means I can't access it by, say: $client->items->email. So my question how do I access the properties of $client object?

Upvotes: 1

Views: 4181

Answers (1)

clone1018
clone1018

Reputation: 1267

get() is for getting all of the records based on the query, meaning it'll return something you can loop through, for example if you change var_dump($client->items) to var_dump($client[0]->items it would work. Instead if you need just ONE record, call the Eloquent model with first() instead of get().

Upvotes: 2

Related Questions