1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- namespace App\Traits;
- use ArrayAccess;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\SoftDeletingScope;
- use Illuminate\Support\Facades\Event;
- use InvalidArgumentException;
- use Spatie\EloquentSortable\EloquentModelSortedEvent;
- use Spatie\EloquentSortable\Sortable;
- /*
- * slug
- */
- trait DistSlugTrait
- {
- /*
- * 生成slug
- */
- public static function generateSlug($slug,$id=0) {
- //截取50个字符
- $slug = truncateString($slug, 50);
- $self = new self();
- $model = $self->model();
- $slug = generateSlug($slug);
- for ($i=0;$i<3;$i++) {
- if ($id > 0) {
- $count = $model->where('id','!=',$id)->where('slug', $slug)->where('dist_id', getDistributorId())->count();
- } else {
- $count = $model->where('slug', $slug)->where('dist_id', getDistributorId())->count();
- }
- if ($count == 0) {
- return $slug;
- } else {
- $slug = $slug . '-' . generateRandomString(2);
- }
- }
- return $slug . '-' . generateRandomString(5);
- }
- public static function isRepeatedSlug($id,$slug) {
- $id = intval($id);
- $self = new self();
- $model = $self->model();
- $count = $model->where('id','!=',$id)->where('slug', $slug)->where('dist_id', getDistributorId())->count();
- if ($count > 0) {
- return true;
- } else {
- return false;
- }
- }
- }
|