Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Querying Models

Query models in OpenSearch using the same Eloquent-style syntax you’re familiar with


All

Retrieve all records for a given model:

$products = Product::all();

Equivalent to get() without clauses.

$products = Product::get();

Find

As with Eloquent, you can use the find method to retrieve a model by its primary key (id). The method will return a single model instance or null if no matching model is found.

$product = Product::find('IiLKG38BCOXW3U9a4zcn');

Find the product with id of IiLKG38BCOXW3U9a4zcn and return the model collection, or null if it does not exist.

$product = Product::findOrFail('IiLKG38BCOXW3U9a4zcn');

Find the product with id of IiLKG38BCOXW3U9a4zcn and return the model collection, or throw a ModelNotFoundException if no result is found.


First

As with Eloquent, you can use the first method to retrieve the first model that matches the query. The method will return a single model instance or null if no matching model is found.

$product = Product::where('status',1)->first();

Find the first product with a status of 1 and return the model collection if it exists.

$product = Product::where('status',1)->firstOrFail();

Find the first product with a status of 1 and return the model collection, or throw a ModelNotFoundException if no result is found.


Get

As with Eloquent, you can use the get method to retrieve all models that match the query. The method will return a model collection or an empty collection if no matching models are found.

$product = Product::where('status',1)->get();
{
"index":"products",
"body":{
"query":{
"match":{
"status":1
}
},
"_source":[
"*"
]
}
}