Skip to content
Advertisement

retrieve single row in LARAVELusing eloquent

i am using laravel 5.1 and want to retrieve a single row in the database then manipulate it after.

my current code is

$profiles = Profile::where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)
->get();

foreach($profiles as $profile ){
$data['address'] = $profile->address;
}

why cant i do it like this?

$profiles = Profile::where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)
->get();

$data['address'] = $profiles->address;

am i using a wrong function or something? thanks in advance.

Advertisement

Answer

Try this:

$data['address'] = $profiles[0]->address;

When you are using get(), it returns an array of Std class object.

In addition to retrieving all of the records for a given table, you may also retrieve single records using first. Instead of returning a collection of models, these methods return a single model instance:

// Retrieve the first model matching the query constraints...
$flight = AppFlight::where('active', 1)->first();
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement