Querying Models
Query models in Elasticsearch using the same Eloquent-style syntax you’re familiar with
Retrieve all records for a given model:
$products = Product::all();Equivalent to get() without clauses.
$products = Product::get();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.
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.
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":[ "*" ] }}Query and return DSL output
Section titled “Query and return DSL output”If you are uncertain about the query being generated, you can use the dslQuery() method to build your eloquent query and return the underlying Elasticsearch DSL array.
$dsl = Product::dslQuery()->where('status',1)->get();// return $dsl:{ "index":"products", "body":{ "query":{ "match":{ "status":1 } }, "_source":[ "*" ] }}