Eloquent Search Examples

Small examples

Let's start with a basic scenario, A tenant has N users, each user has a config and a profile:

Image for post
Image for post
  • Query 1:

Bring all users wich have:

user column is_active in config relationship equals true
user column address in profile relationship contains “Foo”
user column tenant_id equals 1

Pure Eloquent:

Eloquent Search:

  • Query 2

Bring a list of user pictures, from a specific tenant, following the conditions:

user column is_active in config equals false
user column created_at must be greater than equal 2000–01–01 or null
Picture is a column in profile table,

Pure Eloquent:

Eloquent Search

  • Query 3

Search all profiles wich:
- Columns address, street and number contains at least one word of this sentence “Lorem Ipsum Ignolis

Eloquent Search:

In profile Model:

...Profile::searchText('Lorem Ipsum Ignolis')->get();
  • Query 4

Search users with ID greater than 100
Exclude users with ID is between 200 and 300
Exclude users with status_id is in [1,4,6]

  • Query 5

Bring all users wich profile.cash_amount is different from zero OR users with column is_superadmin equals true

Pure Eloquent:

Eloquent Search

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store