How to Generate Unique Slug in Laravel Automatically Easy Way

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Yamin and in this tutorial I am going to show you how you can automatically make slack in laravel so why do we need to make slack so normally here each stop ID in the URL will show a unique title for each post so for in order to show unique title we need to use slag in laravel and this is also ACO friendly as well so right now first of all in order to create slack obviously first of all you need to create a slack column in that table so here you can see I have created a post table over here and in this post table we have title decision and the slack field and when you are creating the slack field you need to make sure that the type for the slack fill is unique here you can see title description and here you can see slot and the slack is unique you need to make sure this so if you already create a table and you do not know how you can include a new column in the table then you can check out this video in the corner where I show how you can add new column to existing table okay so once you have added the slack column and make it unique what you need to do is that you need to go to this table module so here you can see I have a model over here and in the model we have post.php so in this post model you need to write four line of code so here first of all at the top you need to write use CB Pro backslash eloquent flagable backslash we'll see over here flagable after this we'll come to this model over here and after use Factory with our here use sluggable and then we write a function for flag so we server here public function slagable then we'll give to fast bracket on clone and with our hair array then we'll give to Second bracket over here and here we are going to say return then we'll give two third bracket over here and inside this third bracket and for the start bracket we need to give a semicolon over here and inside the start blockade we will give to single quotation and with our hair slack equal is greater than sign and then we'll again give to third bracket over here like this let's just write it like this and inside this third bracket with our here Source equal to is greater than and here you need to mention using which column you want to make the slack so normally we make slack using the push titles so here uh you can see we have a title till over here so using the title field we will create our unique slot so every time someone upload a data then using this title it will automatically create a slug so here we'll say Source equal it will be title and one more thing that unit 2 is that here you can see elupians luggable and here you can see CV broke so we need to install this package in our laravel project in order to automatically make slack so right now we'll go to our common prompt and here we'll write this command composer require let me just zoom in require CP broke backslash with our hair eloquent eloquent minus which over here sluggable and after this we'll just press enter so it will install this package CV broke allowance lagable in our laravel project so here you can see the package is installed and when the package is installed it means the code for creating slag automatically is actually done so right now uh here you can see that I have a input field over here title and description so if every time I add or I upload a data in the database table what uh it will do is that here since we mentioned in the post model slack equal to Source will be title so every time we upload a data it will get the title and make it into a slack and if there is any multiple let's say multiple same title then audit will do is that here you can see address so easing added after that title it will add two three four like this so this it will make every title unique so let me just show you my code over here so here you can see that I have a form over here and in the form I have the title description and the add button over here and here you can see in the form action you can see URL add post so if you go to the web.php and here you can see for add post we are calling this function in the home controller which is at post so in the home here you can see in this public function add post we are just adding the data title and the description only the title and decision we are not adding any slag over here since we mentioned the slack in the post dot PHP in the model so slack data will be automatically uploaded using the title field over here okay so right now if you go to our browser over here let's refresh and let's just give a title over here let's say title will be hello world and let me just copy some text from here Ctrl C and I'll paste it over here so right now if I click add here you can see it's reloading and the data is uploading the database table so right now if I just refresh I refresh my database table here you can see we have the title hello world description and here you can see the slack become hello minus or here you can see it will automatically add this minus sign and right now let's say if we upload a duplicate title again let's say hello again over here and if we just copy this and paste over here and then if I just click add what it will do is that let me just refresh here you can see that it added a two over here at the end of the title so this way a bit like will be evening so right now let me just show you how you can show data using slack so here you can see show post over here if I click show post it will take me to another page and here you can see view details so I went to anything uh any code for this view detail so right now let me just take you to this space code so over here if I just go to post underscore data here you can see the table over here title description end up give details so we are showing the title and description so for few details uh we want to show the details of a specific post using slack so in order to do that we will give two second bracket in this anchor tag then which is our URL to first bracket to single quotation and lists our route name will be post underscore details and we want to show it using slack so instead of getting the ID we'll measure over here dollar data this Arrow sign slack so this taller data is basically this one in the Forest Loop so right now what we'll do is that we will go to our web.php and write this route so let's go to web.php and let you just copy a route from here then I'll paste it over here so right now here we are going to say post underscore details post underscore details and lesser function name will be post underscore details as well and since we are sending the slack form here so we need to Cache the slag over here so we'll give a slash and then we'll shower here flag so we are not sending any ID form here we are sending the slack so right now we'll go to our home controller and create a function post details so let's go to home controller and here we'll say public function first underscore details to flash bracket and here will just say dollar slack which we just mentioned over here you can see slug so right now let's skip to Second bracket so we need to mention a variable over here let's say dollar data or dollar post whatever then here we will mention the table model name double clone normally we write over here find dollar ID part since this is a slot so if we write over here find dollar ID it will not work so we need to see over here wire to five bracket then we need to mention the table column name so here you can see that the table column name is slack so we'll just mention over here double quotation slack then we'll give a comma over here and we will mention this slack this dollar slack over here it means if the slack matches with a column then we'll get that column data so here we'll show over here first you must need to mention the first over here so after this we can just return the view whichever here return View and let's our beginning will be Details page let's say details underscore page and whichever here compact to first bracket to single quotation and here we send this dollar data over here so right now let me just create a simple detail space blades in the view so here in The View we'll see over here details underscore page dot play dot PHP Ctrl s and Save so here we are going to mention the HTML tag and then inside the body we'll give to Second bracket so here from the home controller we are sending the data so we will just keep say over here dollar data error sign with over here title and the same way with server here description I am not adding any design over here descriptions we can just add a br tag over here if we want so here let me just explain what will happen is that here you can see ah we are saying over here view detail for this button we are saying a post detailed dollar data slack it means stop ID it will show us the slack in the URL here you can see Slash post detail slack so in the URL we'll see the slack instruct ID so right now we save this and let me just refresh over here and if I now click over here on give details here you can see the post title and description and here you can see the slag over here not any ID so right now if I just click it for the second post here you can see another slag over here so this is how you can make slack in laravel automatically I hope this tutorial is useful to you if you have any question you can you can always let me know in the comment section and if you want me to make any special video for you you can also let me know in the comment section thanks in for watching I hope to see you in the future tutorials
Info
Channel: Web Tech Knowledge
Views: 2,962
Rating: undefined out of 5
Keywords: how to generate unique slug in laravel, generate unique slug in laravel, how to generate user friendly url in laravel, generate slug in laravel, how to add pretty url in laravel app, how to create slug in laravel, how to generate custom unique id in laravel, how to generate slug in laravel, how to generate link in laravel, How to Generate Unique Slug in Laravel automatically, Laravel Generate Unique Slug Example, Generate a Unique slug From Title, Laravel Create Unique Slug
Id: JZ0TwtAtY9E
Channel Id: undefined
Length: 10min 49sec (649 seconds)
Published: Mon Jun 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.