Today, We want to share with you PHP Creating Unique Title Slugs in Laravel.In this post we will show you Creating Unique Title Slugs with Laravel, hear for Convert string to slug and other custom string functions we will give you demo and example for implement.In this post, we will learn about Create URL Slug from Post Title in Laravel with an example.
PHP Creating Unique Title Slugs in Laravel
There are the Following The simple About PHP Creating Unique Title Slugs in Laravel Full Information With Example and source code.
As I will cover this Post with live Working example to develop PHP Laravel function to make slug (URL string), so the create unique slug in Laravel for this example is following below.
Create unique slug using Laravel and php
The Easiest simle best Way to Make Laravel Unique Slugs for Blog Posts
$title = str_slug("Laravel 5 Framework", "-"); // laravel-5-framework
Laravel Unique slug
Generate a SEO URL friendly “slug” from a given your Title string.
<?php public function show($slug) { $article = Article::where('slug', $slug)->first(); return view('tutorialspoint.single_post', compact('article')); } ?>
And the html Laravel Blade file source code:
<form action="/tutorialspoint/article/store" method="POST"> {{ csrf_field() }} <div class="span12 form-group"> <label for="title">Article Title</label> <input class="span6 form-control" type="text" name="title"> </div> <div class="span12 form-group"> <label for="body">Article Body</label> <textarea class="span6 form-control" name="body" rows="4"></textarea> </div> <button class="span6 btn btn-success" type="submit"> Submit </button> </form>
Define a Laravel Routeing
<?php Route::post('/tutorialspoint/article/store', '[email protected]'); ?>
ArticleController, the store() method.
<?php public function store() { try { Article::create([ 'title' => request('title'), 'body' => request('body'), 'user_id' => auth()->id(), ]); return redirect("/tutorialspoint/article/create")->with([ 'status' => 'success', 'message' => 'Your Article was published successfully', ]); } catch (Exception $e) { return redirect("/tutorialspoint/article/create")->with([ 'status' => 'danger', 'message' => $e->getMessage(), ]); } } ?>
Laravel unique slug First technique
<?php public function defineSlugTitle($title_val) { if (static::whereSlug($gets_slug = str_slug($title_val))->exists()) { $gets_slug = $this->incrementSlug($gets_slug); } $this->attributes['slug'] = $gets_slug; } ?>
And the custom incrementSlug() method:
<?php public function incrementSlug($slug) { $title_total = static::whereTitle($this->title)->latest('id')->skip(1)->value('slug'); if (is_numeric($title_total[-1])) { return pred_replace_callback('/(\d+)$/', function ($relavents) { return $relavents[1] + 1; }, $title_total); } return "{$slug}-2"; } ?>
Laravel Second technique
<?php public function defineSlugTitle($title_val) { if (static::whereSlug($gets_slug = str_slug($title_val))->exists()) { $gets_slug = "{$gets_slug}-{$this->id}"; } $this->attributes['slug'] = $gets_slug; } ?>
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about PHP Creating Unique Title Slugs in Laravel.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโt forget to share.