PHP Creating Unique Title Slugs in Laravel – Technology
In this post we will give you information about PHP Creating Unique Title Slugs in Laravel – Technology. Hear we will give you detail about PHP Creating Unique Title Slugs in Laravel – TechnologyAnd how to use it also give you demo for it if it is necessary.
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'));
}
?>
List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.
And the html Laravel Blade file source code:
<form action="/tutorialspoint/article/store" method="POST">
{{ csrf_field() }}
<div >
<label for="title">Article Title</label>
<input type="text" name="title">
</div>
<div >
<label for="body">Article Body</label>
<textarea name="body" rows="4"></textarea>
</div>
<button 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 :
- Technology
- Google Adsense
- Programming
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 onlinecode 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.
Hope this code and post will helped you for implement PHP Creating Unique Title Slugs in Laravel – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs