Skip to content

Commit

Permalink
Add static slug traits
Browse files Browse the repository at this point in the history
  • Loading branch information
relliv committed May 5, 2023
1 parent e9fdd20 commit 18468c6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ $model->slugNotIn(['any-string', 'any-string']); // will return $query->whereNot

```

### SluggableTitle

This trait allows you to generate a slug from a title field. Same as [Sluggable](#sluggable) trait but it only works with the title field.

```php

use LaravelReady\ModelSupport\Traits\SluggableTitle;
...
```

### SluggableName

This trait allows you to generate a slug from a name field. Same as [Sluggable](#sluggable) trait but it only works with the name field.

```php

use LaravelReady\ModelSupport\Traits\SluggableName;
...
```

### ParentChild

This trait allows you to get all children of the model.
Expand Down
50 changes: 50 additions & 0 deletions src/Traits/SluggableName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace LaravelReady\ModelSupport\Traits;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Builder;

trait SluggableName
{
public function initializeSluggable(): void
{
static::creating(function ($model) {
$model->slug = Str::slug($model->name);
});

static::updating(function ($model) {
$model->slug = Str::slug($model->name);
});
}

public function scopeSlug(mixed $query, string $slug): Builder
{
return $query->where('slug', $slug);
}

public function scopeSlugLike(mixed $query, string $slug): Builder
{
return $query->where('slug', 'like', "%{$slug}%");
}

public function scopeSlugNot(mixed $query, string $slug): Builder
{
return $query->where('slug', '!=', $slug);
}

public function scopeSlugNotLike(mixed $query, string $slug): Builder
{
return $query->where('slug', 'not like', "%{$slug}%");
}

public function scopeSlugIn(mixed $query, array $slugs): Builder
{
return $query->whereIn('slug', $slugs);
}

public function scopeSlugNotIn(mixed $query, array $slugs): Builder
{
return $query->whereNotIn('slug', $slugs);
}
}
50 changes: 50 additions & 0 deletions src/Traits/SluggableTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace LaravelReady\ModelSupport\Traits;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Builder;

trait SluggableTitle
{
public function initializeSluggable(): void
{
static::creating(function ($model) {
$model->slug = Str::slug($model->title);
});

static::updating(function ($model) {
$model->slug = Str::slug($model->title);
});
}

public function scopeSlug(mixed $query, string $slug): Builder
{
return $query->where('slug', $slug);
}

public function scopeSlugLike(mixed $query, string $slug): Builder
{
return $query->where('slug', 'like', "%{$slug}%");
}

public function scopeSlugNot(mixed $query, string $slug): Builder
{
return $query->where('slug', '!=', $slug);
}

public function scopeSlugNotLike(mixed $query, string $slug): Builder
{
return $query->where('slug', 'not like', "%{$slug}%");
}

public function scopeSlugIn(mixed $query, array $slugs): Builder
{
return $query->whereIn('slug', $slugs);
}

public function scopeSlugNotIn(mixed $query, array $slugs): Builder
{
return $query->whereNotIn('slug', $slugs);
}
}

0 comments on commit 18468c6

Please sign in to comment.