Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e50 – SEO Friendly URLs

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

Laravel 5.8 Tutorial From Scratch – e50 – SEO Friendly URLs

today we're gonna be talking about SEO friendly URLs now if you're not familiar with what I'm talking about I'm talking about the fact that sometimes in our

project we just simply have this slash 1/2 something like that with an ID and really those are not great for SEO purposes Google and other search engines

they really want you to have a nice URL so they can identify the content in your website now one example of this that I could give you is in the coders tape website for example up here if you look

at the URL we have a lesson slash than an ID but then we do have introduction to PHP unit setup part one so that way Google kind of knows what's inside this page just by simply having a nice SEO

friendly URL so today I have a fresh project and I want to show you a couple of different examples on how to make SEO friendly URLs with level so let's get right to it

here is my fresh project like I said brand new project I haven't done anything at all the first example that I want to show you is if for example we had this concept of a profile and

instead of having profiles slash and then the profile ID I want to have the user name so going with that let's go ahead and create a model PHP artisan make model for a profile and I do need a

migration for that all right back to phpstorm let's check out the migration for create profile and we're gonna keep it extremely simple we're just gonna have a string for username and it is

gonna be unique and now this is one thing that's important about this technique that I'm showing you this very first technique requires that it be a unique username for example something

like an email that could be unique but if it's something that's provided by the user and you're gonna have several examples of it it just simply would not work I will show you how to do those as

a second example so we'll have a username and just for the sake of it let's have a name for our user that way we do know that we are retrieving the correct user great so now let's go ahead

and create a controller profiles controller all right so let's go back to phpstorm and check out profiles controller in here I will just have a show as that's what I'm

interested in and in my show view of course I will have just for now I will go ahead and say profile I won't use route model binding just yet and let's just simply return profile that's all I

want to do again this is a little bit of setup so we can get this example going here and let's go ahead and add a route for profile slash and then we know we're gonna have the ID of the profile all

simple stuff all stuff that we've covered before encoder stick so profiles controller at show so now in the browser we can just visit slash profile slash one so obviously we don't have any

profiles just yet so let's go ahead and migrate our database migrated and now that we are migrated we can run tinker PHP artisan tinker and let's create a new profile so let's say profile new

profile let's set our username equal to maybe just user name and then let's set the name equal to Victor I'll just keep it simple like that and so let's go ahead and save

that to the database and there we go all right so now if we go back to our browser if I visit profile one then sure enough I get one right whatever I am passing through here that is just simply

what is passing through here okay so now that we have all of that set up let's go back here so of course we know that this is gonna be an ID in typical fashion typically you would say something like

profile is gonna equal to profile right and then we're going to import that up here at the top and then we'll go ahead and find the profile with that ID and then I can just

return profile does that work yes it does so we have our record back so if we wanted to say profile slash username right pertaining to this username right here then of course we could say profile

where username is equal to profile let's go ahead and fetch the first record of that and if we visit profile slash username sure enough we still get the same thing but what about route model

binding isn't it awesome that we could just say well this is a profile laravel you go ahead and handle everything for us right this is kind of what we want to do but if we try that as username of

course that doesn't work we are stuck with that slash one so here's how you make that work if we jump into my profile model and we actually go into the parent model which is this one and

we do a search here for a get route key name and there it is so we can override this get route key name and set up whatever we actually want the column to be so check this out it's actually quite

simple in my profile I'm gonna go ahead and overwrite that get route key name and all I need to do here is return a string with the column that I want to use in my case I want to use username so

if we do that we can actually revert back to using our username and there it is so how cool is that so that's how we make route model binding do we needed to do and we get to set what

we want to do but this breaks down fairly quickly because this only really works if you have a unique constraint on that database but sometimes if you're doing for example blog posts a title may

be used more than once there's no reason why a user could not name two blog posts exactly the same title so why don't we look at an example for that let's go ahead and exit out of tinker here and

let's create a new model and this one is going to be for posts and I do need a migration for that and while we are at it let's go ahead and make a controller because I do know I'm gonna need one of

those so my post controller alright let's head back to phpstorm and take a look at that example let's go to the migration create post table and

let's keep this very simple and just have a title that's all I want to have so in my post controller I will also just have a show and we know we're gonna have route model bindings so why don't

we say post posts for that that way we are taking advantage of route model binding we'll just return the post just to make sure that we did fetch the correct thing from our database so I

need a new route and it's going to be for posts and it's gonna have a post and this is going to be in my posts controller okay so let's head back here and run PHP

artisan migrate and now we have our post table so let's go ahead and create one now PHP artisan tinker and let's create a new post and all we have for this post is just a title so I will just say my

cool title and then let's go ahead and save that to the database there we go alright so we are good to go on that let's head back to chrome and I will go to slash post slash one and if we go in

here it looks like we didn't import that properly let's go back here to my post controller yep alright let's go ahead an import post up here at the top there we go so that will work and refresh and

there we go alright so we are properly fetching that from our database so my cool title the nice thing with this title again is that it could be used more than once so

we can't just simply rely on something like my cool title because if we did then we would not be able to have repeated posts so one trick that I found to be very useful is to have the ID and

then a dash and then a Slugger fide version of the title my cool title how about that now the nice thing with this is that you actually don't have to do a lot of work to make it work check this

out if we go back to phpstorm we go into our web browser file we're gonna have post slash then our post meaning the ID then let's tell level we're actually gonna have a – and after

the – I'm gonna have a slug and that's it as long as we have a slug right afterwards it doesn't matter what we do this so one – my cool title in slug form and

if we check that out yep it still works to show you the difference if I remove that all together and I hit refresh now we get a 404 not found so even though we still have that one it's not able to

match to my route now if I add the dash and then a slug for a variable and we hit refresh yep and we really don't care about the slug I mean you could theoretically short pull in the slug if

you wanted to in your post controller right here we could pull that in as slug but we really don't even need it this is really meant for just SEO purposes again the nice thing with this is that if we

had another post let's go ahead and add another post with the same exact title so now we have two posts right and I'll show you that by just calling post all we have two posts in our database and

both of them have the exact same title but they won't conflict check it out if we hit refresh here yep that still works and if we had a two for the second ID that also still works so this is nice

because it actually eliminates the need to have something funky like one and then – two – three you don't need to do that obviously your ID is always going to be unique so that's nice

we're taking advantage of that unique ID to have a nice SEO friendly URL all right one more trick that I want to show you and it is the following of course once you get into creating these more

complex routes you don't want to have to type this in every single time that you need that route so what we can do is I will actually pull up the Welcome that blade this is just what ships with

Lenovo and somewhere down here of course it says levo let me go ahead and delete everything off of this and all I'm gonna do here is I'm going to add a link and I just want to link to slash post / 1 – my

cool title very complex right I'd really don't want to be typing this up every single time so I'll just name this link so we have this set up here let's go to my post controller and instead of

returning the post let's go ahead and return a view of welcome and with that being set let's go ahead and refresh this and of course if we hit link we're basically pointing to itself and that's

okay we see that that link is working so one cool thing that you can do is the following let's go ahead and pass that post inside our view and then in my post model what I can do is a nice little

helper to help me know exactly what the route is for that post so let's go to post that PHP and you can call this whatever you want typically I just call it path you can call it URL that's the

nation that you can name this whatever you'd like let's just call it path and in here we can return the actual URL that we're going to use now if you want a fully qualified URL you can actually

use the URL helper and then inside of here we know we're gonna have slash post slash and then the post ID so we could say this ID remember we are inside a post at this point and then we're going

to have a slash and then we're going to have this log applied title so we can use s2 you are that's illuminate support STR notice it's being imported up here at the top

and then we can call slug and on that we can pass this title so this is a nice little helper now every time I need to know what the path is to this post all I need to do let's go back to my welcome

is call my post and that will give me the path to this post every single time hit refresh and if we look click on it yep it still works how cool is that so now we have a nice

way if tomorrow for some odd reason I decided to change the way that I actually see that then all I need to do is go ahead and just change it in this one place right here and we got brand

new URLs across the entire site I never hard-coded an actual URL into my view I'm simply just calling this path again you can call this whatever you'd like and you could get the fully qualified

URL to this particular resource I hope that helps and gives you a nice visual on the two most common ways that I use to create SEO friendly URLs as always if you have any questions drop them in the

comments below don't forget to subscribe my name is Victor thanks for watching

Search Engines prefer that your application have nice URLs instead of just an ID of a resource. In this lesson, we are exploring 2 ways to achieve this in Laravel.

For the best experience, follow along in our interactive school at https://www.coderstape.com

DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d

Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch

Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)

About This Course

Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue