Mike Thrussell
Mike Thrussell

Reputation: 4515

Laravel 5.5 eloquent value() only pulling through first row

$origin = User::find($request->user_id);

echo $origin->postcode;

is giving the correct data, whereas:

$origin = User::find($request->user_id)->value('postcode');

echo $origin;

is giving incorrect data as it pulls through the first row only, ignoring the user_id.

Same thing happens with ->pluck('postcode');

This seems unexpected, what is happening here?

Upvotes: 1

Views: 548

Answers (1)

u_mulder
u_mulder

Reputation: 54841

So, evidently method value() does not what you expect.

Diving deep in Laravel core will show you that calling value() of Model instance leads to calling value() method of a Illuminate\Database\Eloquent\Builder, which is defined as:

/**
 * Get a single column's value from the first result of a query.
 *
 * @param  string  $column
 * @return mixed
 */
public function value($column)

and returns just what it should.

So, you chose a wrong method.

Upvotes: 4

Related Questions