Laravelvideo· May 8, 2025· 18:15· 16.6K views on YouTube

Next.js and Laravel Can STILL Be Friends

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

44 moments
  1. Using React with Inertia is a great way to write your front end in Laravel as a full stack framework without having to worry about that interconnecting API layer. We actually have a lot of great videos that you can find in the description below talking about how you can use React with Inertia to write your front end in Laravel. But unless you're starting out your application from scratch with Laravel or you don't have a database just yet, then it can be tricky to know, okay, where does Laravel fit in and how can I start using something like inertia with Laravel. But the good news is that's not all that Laravel can do.

  2. Laravel can work perfectly great just as an API layer for you to incrementally adopt. And maybe it's as simple as a few CRUD actions here or there. Maybe it's a queue. Maybe it's eventually moving everything over so all of your authentication and everything else is handled through Laravel. But how do you get started? How can a NexJS application work with Laravel? First, I just want to start extremely simple. How can we fetch data from a Laravel API, get it up and running and hosted just so we can receive that information in our Next.js application.

  3. We're going to take this video in three parts. first being able to just fetch data from our Laravel API and just receive information without any kind of authorization layer. Next, we're going to look into what it looks like to host that application. And then lastly, we'll get into the weeds of what authorization might look like even if your users accounts don't live in your Laraveville application. I have a directory here that we can install our Next.js application and our Laravel application just to make it easier while we're in development. npx create next app at latest.

  4. I'm just going to call this next 15. All of the defaults. And next, we'll install the Laravel application. I'm just going to say Laravel new API. We're not going to use a starter kit, but we will install Sanctum in the next step. And then all the defaults for this are fine as well.

  5. Now, we can get both of these applications up and running. I'm going to CD into API. I'm going to run composerdev just to run the PHP artisan serve server. And then in the uh Nex.js JS application directory. We're going to run mpm rundev. So I'm going to go into the next 15 directory and I'm going to say uh mpm rundev. All right. So our API is living on port 8000 for localhost and then our nextjs application is being served at port 3000.

  6. So how can we fetch information? Well, there's a couple of ways to do it. So it kind of is up to your personal preference and what works best for your application. But I'll show you the most usual ways. And before we even create an API route in our Laravel application, let's install the API setup for us. And Laravel gives us an easy way to do this out of the box. We can just say PHP artisan install API. And what this is going to do is it's going to do a few things.

  7. It's going to install Laravel Sanctum and then just set all set up all the things we might need for most API use cases. This is going to gives us um some uh database migrations to run. In this case, a personal access tokens table for if you're using Sanctum to actually authenticate within Laravel.

  8. And then in our API.php, that's the route file, we can go ahead and add a new route. In this case, I'm just going to say /high. It just returns a response of JSON with a message and a description. Now, you can use whatever application you would like to kind of test to make sure this is working, but this is all just fairly basic for now. I'm going to use HTTP, which is one of my favorite applications to kind of test this out. So, if we go ahead and send that request, then we get that message back as well as the description.

  9. And of course, we can add whatever parameters we would like in this particular route. We're not doing any kind of authentication or authorization within this particular route. We're just saying, hey, I want to receive some information and then, you know, do whatever we want with it on the front end. So, how does this look like in an XJS application? Let's take a look. So, this would be how you would fetch data from the server within an X.js application. So, this isn't happening client side.

  10. This is rather happening before the page even loads or during the request, I should say, on the server. it's receiving that response from our particular API and then we're just waiting for that response so that we can display it on the page. The neat thing with Nex.js, as you might know, is if you have a a loading.tsx, this is going to display what you want to load or show the user while any of your server requests are loading.

  11. So we could export a default uh function that's is just a loading function and just say hey loading this info. Perfect. So if we take a look at what this looks like now in our next.js application and we refresh, we have that 1 second of delay before we actually load that information from our API. Now if only our applications were this simple.

  12. If we were just retrieving any kind of information from a public-f facing API without having to authenticate or authorize any kind of user to make sure that they're able to receive that information, then this would be great. Laravel makes complete sense. And to be fair, you're not using really any of Laravel's fantastic features. It just makes a lot easier when you do want to use those features down the road if you're maybe sending an email from this API. But again, what happens when you need to authenticate or authorize a user to do so?

  13. Real quick, if you're a web artisan who just wants to ship, like and subscribe for more content on learning to do just that. One small change in our next.js application as I'm pushing this up to production is we don't want to have to type out that API URL every single time, especially when it's going to be different in production than it is in development. So we can use uh process aenvariable in order to retrieve that URL. So locally.local, we're just going to use that next public API URL.

  14. But now that we have our Laravel application, our API hosted on cloud, we can use that in whatever hosting application we're using for Nex.js. I'm using forcell for right now. So I already have my Laravel API hosted and pushed to cloud. Extremely simple. We're not doing anything fancy.

  15. just yet. So, the default options are perfect for us. But all we need to do is just make sure that this uh this domain right here slappi is now in our environment variables wherever we're hosting our next.js application. Just like this. And then we should be good to go. And there we go. Our application is now hosted. Our API is now hosted on Laravel cloud. And we're retrieving that data from where our NextJS application is hosted from.

  16. So even if I was to go back into HTTP PI and say okay let me just make sure that this publicfacing API is working as it should. I can just go ahead and paste in that cloud URL. Go to /api/h high and let's see there we go. We're getting that same information.

  17. Our API is public publicly accessible on Laravel cloud. And now our Nex.js application is also retrieving that information. And of course Nex.js XJS is doing some fancy stuff in the background to cache this information. So we don't even see that loading because uh we haven't broken that cache just yet. And of course that's stuff that you have to think about for your application. But this is just basic data fetching on the server. Of course there are ways to do this on the client.

  18. And that the the difference in how you're going to be doing this on the Laravel side is not going to be too different. You're just going to be setting that same API. It's just going to be different on how you would accept that information or actually request that information from the next.js side.

  19. Some popular options from the Nex.js documentation for client components are using like reacts use hook which in this case it kind of just defaults to any kind of suspense and streaming and your API on Laravel side is not going to change at all. you're just going to be using that instead of something like Axios or of course there's community libraries like SWR or React Query by the Tanstack team.

  20. Both of these are fantastic options and again this depends on what part of your application do you actually need this information. We're going to talk about mutations in order to do any kind of CRUD events especially with authorization pieces. And a lot of that is easier with something like SWR or React Query, but it depends on your personal preferences. It depends on what your application actually needs. Is this going to be doing it on the server within Nex.js or on the client.

  21. So now that we have this up and running, what does it look like to incrementally adopt Laravel in a more advanced or maybe uh more fully featured Nex.js JS application where now you're like okay I need cues or I need background jobs that are running and I might as well push that all to Laravel. So but maybe my full database and everything isn't on Laravel just yet.

  22. How would we do some of that authorization piece and incrementally adopt Laravel for the things that we need on the back end? Well, each use case is completely different, but hopefully this gives you a broad overview of the things to think about and the things you might actually need to change and complete for when that time comes in order to use Laravel's API to do all the wonderful magic that Laravel gives on the back end to consume that or to do all the things that you need to do within your Nex.js application.

  23. And even though Laravel is totally capable to be an API in that sense of just sending information from a singular API endpoint without any kind of authorization or bells and whistles, maybe you just want to check, hey, is PHP still relevant? And it returns a JSON responsive. Yes, but Laravel also has a lot more options when it comes to what is built into the framework itself.

  24. And so what if you wanted to use some of those features within your existing Nex.js JS app, but you can also use the incredible array of features that Laravel's framework provides, even if you're just consuming them from an API.

  25. Now, I'm not going to go extremely in-depth on how you might be able to replace a feature or a service within your Nex.js application, but let us know in the comments if that is a video that you would like to see in the future of how to replace like your Q service with Laravel's Q's and we can swap that out with any open-source Nex.js JS application, but I do want to take some time to show how you might use an external service that you're already using in your Nex.js application, maybe an external login and with some identifiers for your users.

  26. You can say, hey, this is the authorization that I'm giving them within my Laraveville API.

  27. And right now, we're just going to take a look at how you might have your users log in and then create a Sanctum API token that you can consume on your Nex.js JS front end but then also pass back to Laravel whenever you need it to maybe create bookmarks for example for your users extremely highle overview and the repo for this project is in the

  28. description below but this is how you might use Sanctum for API tokens specifically you're already authenticating but we're passing in some kind of identifier to create an API token for your users and if you're not familiar with Sanctum Sanctum gives you the ability to create API tokens or uh the ability to have SPA authentication, cookiebased session authentication for your APIs.

  29. And one note that you might need for this, and like this note says, it's perfectly fine to use Sanctum just for API token authentication or you can also use Sanctum for that cookie based authentication. You can use one or the other or both. It doesn't really matter.

  30. You can choose within Sanctum. We already installed it previously with that command that we ran. And just a note, if you did want to use it for that SPA cookiebased authentication and not just for the API token authentication that we're showing here, you do have to make sure that your SPA and your API shares the same tople domain. But real quick in the Laravel code before we jump into the next.js application and then show what this looks like live, we just created a route post that logs this person in. In this case, we get an email and a name from them.

  31. We're just assuming that you've already authenticated them on your client or whatever external service here. But then we're just saying, hey, we're going to give them a token. We're going to pass that back with the response. And then in the nextJS application, we can do with that as we wish. For right now, we're just setting it as a cookie so that when we send another request to the Laravel API, we can send it as part of that header because that is what Sanctum is expecting.

  32. Then we have some bookmarks route a a model in our Laravel API that we can create bookmarks with our users and this is using the O sanctum middleware to say okay they do need to be authenticated and because we're using the API tokens to do that we can make sure that they are in fact authenticated. Both of these routes look pretty familiar because they look exactly what you might expect outside of a larville API. Now let's take a look at what the Nex.js JS application looks like to consume and create these bookmarks as well.

  33. Now, this is where depending on your preferences or just depending on how you might want to run specific functions within your Nex.js application, it can be a client side request or server side request or an intermingled use of the two. And right now, that's what I'm kind of showing, an intermingled use of the two. And of course you can use something like axios, you can use something like tanstack react query. You can use something like swr or even something like kai ky which is an axio alternative.

  34. I'm just using straight plain fetch to kind of show how to do these requests to the API. But here I'm just saying for this external login I am logging the user in with that information that we are kind of presenting and saying that okay right here yes we're hard coding this but this is might what might you might retrieve from your external service if your users are already authenticated and then we're

  35. retrieving that JSON information that cookie that we're or that token that we're sending back from the Laravel API and we are setting that up as a cookie in our Next.js application and then we're just going to bring the person to the dashboard. Now, what does that dashboard look like? Now, here's where we actually have a server rendered page.

  36. So, when this page renders, we're going to grab same thing like how we showed at the beginning of this video. You're going to fetch information from this user using that cookie, that API token that we set previously. Uh we're going to send that to this bookmark. That's where we are grabbing the tokens or grabbing all of the bookmarks and sending them back to the page. And then lastly, our bookmark form, which is a a client form. Again, using fetch to be able to send out the information.

  37. In this case, a simple post request to say I want to create a new bookmark with the URL and the title that we have provided. Now, yes, all of this is mostly just to show how it might be possible if you were using some external service to already authenticate your users and you just needed that API token because you didn't want to maybe have the users information split between two databases.

  38. The one database that Laravel uh connects to and one database that your off service connects to. But as a simple way to make sure that someone is able to complete a request that you're wanting them to complete, API tokens through Sanctum do a fantastic job. And storing them as cookies might not be what works best for your application. Again, just showing how this might work so that you can then say, "Now I have this token.

  39. I can have this user start a queue or I can send an email to this user because I already have their email from that token." You can see how the possibilities are starting to line up.

  40. So why don't we see what this actually looks like in the wild. Pretending that this is an external O portal. When I click sign in, what we are doing is accessing the token. And if one is not already created, then we're creating one again just with that email that we are provided. But if there was already one created, we're grabbing that token and then passing it and setting it as a cookie. You can see here that if I was to go into application and cookies, we have that API token saved as a cookie in our in our application settings.

  41. And this is a bookmark I already created for this user. Now, if I wanted to add another one, I'm just going to add the URL of this actual application. This is our uh bookmark dashboard. Add a bookmark. And there we go. And because this is a server page, because we refreshed that page, it's grabbing those new bar bookmarks on page refresh.

  42. You kind of saw that loading flash on the top left, that's what we had created, that loading.tsx page from the initial get-go. And since that fake login that we created just relies on a email, any email, you can see here that if we were to change this maybe to uh taylorwell.com, it creates a new user that doesn't have any existing bookmarks from our old user.

  43. Laravel is an incredibly powerful full stack solution, but it also works incredibly well even if you're using it just for simple APIs or maybe even complex APIs down the road. And built with something like Sanctum, which gives you that API based token authentication or SPA cookie based authentication, it creates a powerful uh duo, a powerful pair with something like Nex.js. So yes, Nex.js JS and Laravel can still be friends.

  44. \[Music\]

just you here