Last active
April 22, 2018 00:16
-
-
Save janzikmund/8eccb1516c1183728d8e0f5ad8e862e3 to your computer and use it in GitHub Desktop.
Laravel editable slugs for resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- shared form for both add and edit --> | |
<div class="form-group row {{ $errors->has('slug') ? 'has-danger' : '' }}"> | |
<label class="col-2 col-form-label">Slug</label> | |
<div class="col-10"> | |
<input class="form-control {{ $errors->has('slug') ? 'form-control-danger' : '' }}" type="text" value="{{ old('slug') ?? $location->slug ?? null }}" name="slug" placeholder="[generate automatically]"> | |
@if($errors->has('slug')) | |
<div class="form-control-feedback">{{ $errors->first('slug') }}</div> | |
@endif | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ----------- CONTROLLER ----------- | |
/** | |
* Store resource | |
*/ | |
public function store(Request $request) | |
{ | |
// nothing special, slug generated automatically | |
$location = Location::make($request->all()); | |
$location->save(); | |
return redirect()->route('admin.locations.index'); | |
} | |
/** | |
* Update resource | |
*/ | |
public function update(Request $request, Location $location) | |
{ | |
// if custom slug was passed, slugify it and pass it to validation | |
if($request->filled('slug')) { | |
$request->merge(['slug' => str_slug($request->input('slug'))]); | |
} | |
// validation | |
$this->validate($request, [ | |
'slug' => 'unique:locations,slug,' . $location->id, | |
], [ | |
'slug.unique' => 'This slug has already been taken. Enter a unique one, or submit empty value to auto-generate.', | |
]); | |
// update slug if not empty, otherwise let the library generate one | |
if($request->filled('slug')) { | |
$location->slug = $request->input('slug'); | |
} else { | |
$location->generateSlug(); | |
} | |
$location->save(); | |
return redirect()->route('admin.locations.index'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use Spatie\Sluggable\HasSlug; | |
use Spatie\Sluggable\SlugOptions; | |
class Location extends Model | |
{ | |
use HasSlug; | |
protected $fillable = ['name', 'slug']; | |
/** | |
* Get the options for generating the slug. | |
*/ | |
public function getSlugOptions() : SlugOptions | |
{ | |
return SlugOptions::create() | |
->generateSlugsFrom('name') | |
->saveSlugsTo('slug') | |
->doNotGenerateSlugsOnUpdate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment