Laravel update multiple rows with different values
Discover efficient techniques for updating multiple rows with unique values in Laravel. Explore step-by-step instructions and practical examples to streamline your database operations.
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
trait HasUuid
{
/**
* Generate a new UUID for the given model.
*
* @return void
*/
public static function bootHasUuid(): void
{
static::creating(function (Model $model) {
$model->uuid = self::generateUuid();
});
static::updating(function (Model $model) {
if (empty($model->uuid)) {
$model->uuid = self::generateUuid();
}
});
}
/**
* Generate a random UUID.
*
* @return string
*/
public static function generateUuid(): string
{
do {
$uuid = (string) Str::uuid();
$exists = self::where('uuid', $uuid)->exists();
} while ($exists);
return $uuid;
}
}
Discover efficient techniques for updating multiple rows with unique values in Laravel. Explore step-by-step instructions and practical examples to streamline your database operations.
Laravel aliases are shortcuts for long namespaces in your code. They make your code cleaner and more readable, enhancing developer productivity.
How to setup a Laravel Markdown editor and convert the markdown to HTML easy.