Friday, March 31, 2017

Laravel searching on hasone relationship

I want to be able to perform search on values in hasOne relationship.

I have deliverables and deliverable_versions table with hasMany relationship. So a deliverable can have many deliverable_versions.

I also have deliverable hasOne deliverable_version to get the latest version.

I want to be able to list the deliverables, but after performing a search on the latest version.

The problem is, in my current database, the versions have all the information needed in search.

So for example, I want to search on the values on "recent version" like this.

$deliverables = $account->deliverables()->with('recentVersion');
$deliverables = $deliverables->search($search);
$deliverables = $deliverables->paginate(4);

How can I achieve something like this in Laravel Eloquent?

Any help is appreciated.



via Seyong Cho

Advertisement