Sangar82
Sangar82

Reputation: 5230

Eloquent ORM: Obtain a object, not an array of objects

I have this statement in Laravel 4:

$prepayment = Prepayment::where('hash', '=', $custom)->take(1)->get();

But this return an array of objects, and I want only one object, because there are only one result with the hash that I search.

How is the way to obtain only one object, not an array of objects?

Thanks!

Upvotes: 2

Views: 2659

Answers (2)

Roj Vroemen
Roj Vroemen

Reputation: 1892

Use the first() method instead of the get() method

$prepayment = Prepayment::where('hash', '=', $custom)->first();

Upvotes: 11

Thomas Clarkson
Thomas Clarkson

Reputation: 755

This is a good article to learn more about how the eloquent orm works. http://laravel.io/topic/14/how-eloquent-relationship-methods-work It describes how the belongs to relationship will just give you one object whereas the has many relationship will give you an arrray of objects

Upvotes: 0

Related Questions