Skip to main content

Retrieving Models

The PHP models provide a fluent syntax to query the database over the api. In this doc, we will show a variety of ways to retrieve models.

Retrieving a model by id

The model's all method will retrieve the record with a specific id.

use SureCart\Models\Product;
$product = Product::find('8ba8d60f-5277-4e6b-807c-dee8166446d5');

Retrieving several models

The model's all method will retrieve the first 10 of the records from the model's associated database table:

use SureCart\Models\Product;

$products = Product::get();
foreach ($products as $product) {
echo $product->name;
}

Building queries

In order to query by specific parameters, you can use the where method. This will send query parameters over the API and return the matching records.

use SureCart\Models\Product;
$archived_products = Product::where([
'archived' => false
])->get();

Pagination

You can paginate and get subsquent pages of results by using the paginate method. This method takes 2 parameters, page and per_page. The default pagination per_page limit is 100.

use SureCart\Models\Product;

$archived_products = Product::where([
'archived' => false
])->paginate([
'per_page' => 20,
'page' => 2
]);

Result:

[
'object' => 'product',
'pagination' => [
'count' => 100,
'limit' => 20,
'page' => 2
],
'data' => [
// 20 products
[
'id' => '8ba8d60f-5277-4e6b-807c-dee8166446d5',
...
]
...
]
]

Refreshing Models

If you already have an instance of a model that was retrieved, you can "refresh" the model using the fresh method. The fresh method will re-retrieve the model from the database. The existing model instance will not be affected:

$product = Product::create([
'name' => 'New Product',
]);
$product->name = 'Updated Product';
$freshProduct = $product->fresh();
$product->name; // "Updated Product"
$freshProduct->name; // "New Product"

The refresh method will re-hydrate the existing model using fresh data from the database:

$product = Product::create([
'name' => 'New Product',
]);
$product->name = 'Updated Product';
$product->refresh();
$product->name; // "New Product"