Skip to content

Instantly share code, notes, and snippets.

@bolechen
Last active October 12, 2020 10:58
Show Gist options
  • Save bolechen/ca98c08ba8574d93954bdcc3e9006e3f to your computer and use it in GitHub Desktop.
Save bolechen/ca98c08ba8574d93954bdcc3e9006e3f to your computer and use it in GitHub Desktop.

Revisions

  1. bolechen revised this gist Aug 4, 2019. 1 changed file with 35 additions and 1 deletion.
    36 changes: 35 additions & 1 deletion HasUuid.php
    Original file line number Diff line number Diff line change
    @@ -16,10 +16,44 @@ public function getRouteKeyName()
    return 'uuid';
    }

    public static function findByUuidOrFail($uuid)
    {
    return self::whereUuid($uuid)->firstOrFail();
    }

    /**
    * Eloquent scope to look for a given UUID.
    *
    * @param \Illuminate\Database\Eloquent\Builder $query
    * @param string $uuid The UUID to search for
    *
    * @return \Illuminate\Database\Eloquent\Builder
    */
    public function scopeWithUuid($query, $uuid)
    {
    return $query->where('uuid', $uuid);
    }

    /**
    * Eloquent scope to look for multiple given UUIDs.
    *
    * @param \Illuminate\Database\Eloquent\Builder $query
    * @param array $uuids The UUIDs to search for
    *
    * @return \Illuminate\Database\Eloquent\Builder
    */
    public function scopeWithUuids($query, array $uuids)
    {
    return $query->whereIn('uuid', $uuids);
    }

    protected static function bootHasUuid()
    {
    static::creating(function ($model) {
    $model->uuid = Str::orderedUuid();
    if (!$model->uuid) {
    // 这里使用了 orderedUuid 方便后期分表
    $model->uuid = (string) Str::orderedUuid();
    }
    });
    }
    }
  2. bolechen renamed this gist Jul 19, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. bolechen revised this gist Jul 17, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion laravel_uuid_trait.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ public function getRouteKeyName()
    return 'uuid';
    }

    protected static function bootUsesUuid()
    protected static function bootHasUuid()
    {
    static::creating(function ($model) {
    $model->uuid = Str::orderedUuid();
  4. bolechen revised this gist Jul 17, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion laravel_uuid_trait.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    use Illuminate\Support\Str;

    trait UsesUuid
    trait HasUuid
    {
    /**
    * Get the route key for the model.
  5. bolechen revised this gist Apr 15, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions laravel_uuid_trait.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    ```php
    <?php

    namespace App\Traits;
    @@ -24,4 +23,3 @@ protected static function bootUsesUuid()
    });
    }
    }
    ```
  6. bolechen renamed this gist Apr 15, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. bolechen created this gist Apr 15, 2019.
    27 changes: 27 additions & 0 deletions laravel_uuid_trait
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    ```php
    <?php

    namespace App\Traits;

    use Illuminate\Support\Str;

    trait UsesUuid
    {
    /**
    * Get the route key for the model.
    *
    * @return string
    */
    public function getRouteKeyName()
    {
    return 'uuid';
    }

    protected static function bootUsesUuid()
    {
    static::creating(function ($model) {
    $model->uuid = Str::orderedUuid();
    });
    }
    }
    ```