Inserting Or Updating Models
Save
The most common way to create or update a model is to use the save method. This will create a new model if there is no id. Otherwise it will update an existing model if an id is provided.
Create a new product:
use SureCart\Models\Product;
// creates a new product
$product = new Product([
'name' => 'iPhone',
'description' => 'iPhone is a smartphone'
]);
$product->save();
Update an existing product:
// updates an existing product
$product = new Product([
'id' => '8ba8d60f-5277-4e6b-807c-dee8166446d5',
'name' => 'iPhone',
'description' => 'iPhone is a smartphone'
])->save();
Update an existing product:
// updates an existing product
$product = Product::find('8ba8d60f-5277-4e6b-807c-dee8166446d5');
$product->name = 'iPhone 8';
$product->description = 'iPhone is a newer smartphone.';
$product->save();
Create
You can specify that you want to create a model directly by using the create
method.
use SureCart\Models\Product;
$archived_products = Product::create([
'name' => 'iPhone',
'description' => 'iPhone is a smartphone'
]);
Update
You can specify that you want to update a model directly by using the update
method.
use SureCart\Models\Product;
$product = Product::find('8ba8d60f-5277-4e6b-807c-dee8166446d5');
$product->update([
'name' => 'iPhone',
'description' => 'iPhone is a smartphone'
]);