BaseModel::insertOrUpdate
Usage
static BaseModel::insertOrUpdate(array $criteria, array $values)Description
Insert or update a record within the database table assigned to the model class.Parameters
Parameter | Required | Type | Description |
---|---|---|---|
$criteria | Yes | array | The criteria / condition to check whether or not a record already exists. If yes, the record will be updated and otherwise a new record will be inserted. |
$values | Yes | array | All remaining values to create / update the reocrd with. If a new record is being created, both these values plus the `$criteria` values will be used. |
Return Value
An instance of the model class of the created / updated record.Examples
Insert, then Update Category
use App\Demo\Models\Category;
// Insert
$cat = Category::insertOrUpdate(
['slug' => 'games'],
['name' => 'Games']
);
// Update the 'name' of the newly created record
$cat = Category::insertOrUpdate(
['slug' => 'games'],
['name' => 'Video Games']
);
// Result is one record, now with the name 'Video Games'
echo "Category name is $cat->name\n";