Question:
How to create a unique slug or unique url in Laravel From title?Solution:
Suppose I've a Model Product and tables products. In that table, there is a slug and When I'll insert any product by product title, then I'll make a unique slug by using that title.At first We know that how to create a slug in laravel. That's
str_slug($title, $separator="-");
If I've requested a title, then from that title I can make a slug by this -
public function store(Request $request)
{
$title = $request->title;
$slug = str_slug($title, "-");
//Rest of the works...
}
But,
If we create a slug by this that's never be unique. Unique means -
- Add another same level title can create another slug. Like first title = 'my-title' and again I've a post titled by 'my-title' , then if I store it needs to store it like my-title-1 or my-title-2
We'll now create a slug that'll be totally unique.
Let's Start:
Create our own function that generate unique slug from any title. Look at the code and that is pretty much clear.
Full ProductController Class
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProductController extends Controller
{
public function store(Request $request)
{
$product = new Product;
$product->title = $request->title;
//Make a unique product slug
$product->slug = $this->createSlug($request->title);
$product->save();
return redirect('/');
}
//For Generating Unique Slug Our Custom function
public function createSlug($title, $id = 0)
{
// Normalize the title
$slug = str_slug($title);
// Get any that could possibly be related.
// This cuts the queries down by doing it once.
$allSlugs = $this->getRelatedSlugs($slug, $id);
// If we haven't used it before then we are all good.
if (! $allSlugs->contains('slug', $slug)){
return $slug;
}
// Just append numbers like a savage until we find not used.
for ($i = 1; $i <= 10; $i++) {
$newSlug = $slug.'-'.$i;
if (! $allSlugs->contains('slug', $newSlug)) {
return $newSlug;
}
}
throw new \Exception('Can not create a unique slug');
}
protected function getRelatedSlugs($slug, $id = 0)
{
return Product::select('slug')->where('slug', 'like', $slug.'%')
->where('id', '<>', $id)
->get();
}
}
That's it. We've created an Unique slug in Laravel.
So, If we give
- first title = 'Our Product' Then slug will be our our-product
- second title = 'Our Product' Then slug will be our-product-1
- third title = 'Our Product' Then slug will be our-product-2
So, the URL or Slug now is totally Unique. That's the system to create a unique slug in Laravel.
Tags:
How to create a unique slug or unique url in Laravel From title, Laravel create slug, Laravel create URL, How to create a unique slug in Laravel. Laravel Unique URL
How to create a unique slug or unique url in Laravel From title - Laravel Easy Tutorials
Reviewed by Maniruzzaman Akash
on
November 04, 2017
Rating:
Our certified Laravel developers can create scintillating extensions and mapping of web applications that ensure quicker performance. We have the acumen of delivering small scale to large scale projects based on laravel Development. You name the service that you are looking for in Laravel technology and we will be able to deliver that to you in minimal time. Contact@ExpressTechSoftwares.Com or +91-9806724185
ReplyDeleteNice tutorial. Saved my time. Thanks
ReplyDeleteVery helpful article. According to Laraval usage statistics compiled by BuiltWith, Laravel has been steadily growing in popularity, with more than 700,000 live sites using it. In USA, laravel development companies are rapidly growing with its increasing demands.
ReplyDeleteAntalya
ReplyDeleteTrabzon
Niğde
Maraş
Antep
H1G60
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
GG6
0D2B4
ReplyDeleteAfyon Lojistik
Nevşehir Lojistik
Bartın Parça Eşya Taşıma
Bilecik Evden Eve Nakliyat
Muş Lojistik
A1191
ReplyDeletebinance %20 komisyon indirimi
5F4D6
ReplyDeleteKilis En İyi Ücretsiz Sohbet Uygulamaları
sohbet chat
bartın en iyi ücretsiz sohbet siteleri
bayburt sohbet uygulamaları
kocaeli canlı görüntülü sohbet siteleri
bingöl canlı sohbet et
sesli sohbet uygulamaları
tunceli sesli sohbet siteleri
canli sohbet
F0230
ReplyDeleteBitcoin Kazma Siteleri
Tiktok Takipçi Satın Al
Kripto Para Nasıl Alınır
Binance Hangi Ülkenin
Paribu Borsası Güvenilir mi
Görüntülü Sohbet Parasız
Bitcoin Kazanma
NWC Coin Hangi Borsada
Coin Madenciliği Nedir
شركة عزل خزانات بالرس nwDHOAJWFX
ReplyDeleteشركة عزل اسطح بالجبيل QPMSovQrlA
ReplyDeleteشركة تسليك مجاري بالجبيل GsygnzuIiw
ReplyDeleteافضل شركة مكافحة حشرات 1eooct4Z3L
ReplyDeleteشركة تسليك مجاري بالاحساء C6Xuax9Hzz
ReplyDelete