
Create record with unique slug in laravel
You can create a unique slug for a record in the table in laravel. Sometimes you have to create the slug which should be unique so that you can uniquely identify each record without using its id and it’s also required for SEO purposes.
-
Create post with unique slug in laravel 8
//app\Http\Controllers\HomeController.php //Import class after the namespace and before the class definition use Illuminate\Support\Str; use App\Models\Post; //Controller's method public function createSlug($title, $table , $id = 0) { // Normalize the title and create slug $slug = Str::slug($title); // Get any that could possibly be related. // This cuts the queries down by doing it once. $allSlugs = $this->getRelatedSlugs($slug, $table, $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 it is 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'); } //Function to get all the records which has same slug public function getRelatedSlugs($slug, $table, $id = 0) { return DB::table($table)->select('slug')->where('slug', 'like', $slug.'%') ->where('id', '<>', $id) ->get(); } public function createPost(){ $title = 'Nisi iure id ut molestiae quae'; $slug = $this->createSlug($title, 'posts'); $post = Post::create( ['title' => $title, 'slug' => $slug] ); echo $post; } //routes\web.php use App\Http\Controllers\HomeController; Route::get('/create-post', [HomeController::class, 'createPost']);
0Output :
{"title":"Nisi iure id ut molestiae quae","slug":"nisi-iure-id-ut-molestiae-quae","updated_at":"2021-09-16T04:43:04.000000Z",
"created_at":"2021-09-16T04:43:04.000000Z","id":1}You have to create a posts table with a title and slug column. Create the post model with fillable attributes and add title and slug to this fillable attribute. You have to pass the post title and table name to createSlug() method to create the unique slug by appending 1, 2, 3, … if the slug already exists with the same value in the table.
Random Code Snippet Queries: Laravel
- Call to a member function update() on null
- Get the post details if it has at least one comment in comments table
- How to pass variable from controller to model in Laravel
- How to pass query string to url in laravel
- Route [password.request] not defined
- How to add active class to menu item in laravel
- How to check query string exists or not in laravel blade
- How to insert value to additional columns in pivot table in laravel
- Create records using relationship in laravel
- How to add foreign key in laravel using migration
- Cannot end a section without first starting one
- How to validate form input data in laravel
- Get content from web URL in laravel
- How to decrypt laravel password
- How to call controller function from view in Laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Send OTP using textlocal api in laravel
- Use of undefined constant laravel
- How to fill a column automatically while creating records in Laravel
- How to pass data to partial view file in laravel
- Create project table with model and migration
- Credit card validation in laravel
- Laravel insert query not working
- SQLSTATE[42000]: Syntax error or access violation: 1055
- How to validate URL with https using regex in laravel