Laravel vs React
In this episode, I break down the common misconception of Laravel vs React and explain how they can work seamlessly together. We explore Laravel's robust backend capabilities and React's rich front-end interactivity, and how using libraries like Inertia can bridge the gap for a smooth, full-stack development experience.
Everything I said
Laravel versus React. I'm going to tell you why Laravel is awesome and you should be using it, and why React sucks and you shouldn't-- Hang on. Yes, I'm doing a video. Mm-hmm. No . Laravel and React work great together.
Saying Laravel versus React is a false dichotomy, and you don't have to pick between one or the other. You can put them together to get the best of both worlds. The first thing we gotta figure out is this whole front-end versus back-end thing. It's a pretty simple distinction. Back-end code runs on the server. Front-end code runs on the client. That line has actually become a little bit more blurry recently with React server components, for example, where that takes some of that front-end code, and it puts it on the back end.
So now you have front-end code running on the server. We'll talk about some of that nuance here in a second, but for now, let's just say back end is server side, front end is client side, and in the middle is this chasm of the network. So make the back end and front end talk to each other, you have to go over the network. We're gonna start by talking about Laravel. Laravel very much starts at the back end.
It very much starts as a back-end framework. That's not all it does, but that's where it starts. The back-end part of Laravel goes all the way down to the database layer, and then it moves up to models and cron jobs and queues and caching and emails and authentication and routing and so much more until it reaches the network chasm. That is just the back-end part.
That is not where it stops. It does go to the front end. It's got a full templating language. Any-- E- everything you would expect from a front-end templating language. It's got components and layouts and slots and loops and variables and all of that kind of stuff that you have come to expect from a front-end templating language. So if you wanted to build a full stack application with just Laravel, you could.
You absolutely could. You'll notice a few things about the way we drew these graphics. One is that the network chasm still exists. Laravel plus Blade doesn't make that go away. On the back end, you're likely writing controllers that return views or maybe JSON data if it's an API endpoint. And then on the front end, you have to figure out, how do I consume that API endpoint?
Or how do I post a form? Or how do I navigate to an entirely different route? You would do that with a link. But that network chasm still exists. The other thing you might notice is that this front-end bar stops short of the very, very end, and that's because once you need something with rich client-side interactivity, you're gonna have to reach for something besides Blade.
Laravel knows this, and they embrace it fully. They know that people wanna use React or Vue.js on the front end, and you don't have to eject out of Laravel to do that. In fact, there are some really good first-party integrations to make that possible. Let's talk about React and Vue first. React is a client-side library for building user interfaces out of individual pieces called components written in JavaScript.
Vue is very much the same way. It is a client-side library for building out user interfaces. If you need an interactive front end, you're probably gonna reach for something like React or Vue. If you're a Laravel developer, you might reach for something called Livewire, which we're gonna talk about, but let's keep going on React. React started out fully client side, and slowly, it is starting to work its way towards the back end.
With these meta frameworks like Next.js, they're starting to put React server components on the server. So something like Next.js bridges that network chasm, so you can run stuff on the back end without writing controllers or routes or API endpoints or something like that. Remix, on the other hand, claims to be a full stack framework, but its creator says that that was a mistake, and they're not actually a full stack framework.
They're more front end. I have also seen Remix be called a center stack framework because primarily, their job is to bridge that network chasm, and then you would use React on the front end. But on the back end, you're still left with no structure at all, and you're left to figure out databases and mailing and cache and authentication, all of that stuff.
Remix is UI-focused, not back-end focused. My more enterprising viewers probably already know where this is headed, but I'm gonna spell it out for you anyway. Look at the rich client-side interactivity you can get with a library like Vue or React. You can go as far as you want on the client side. Look at the rich, powerful set of back-end tooling you get with something like Laravel, stuff you need to build a real application.
What if we could combine those two? And just for kicks, what if we could combine them in such a way that also eliminates this network chasm? Well, good news, we can. Laravel offers first-party starter kits that include Laravel and Vue or React and bind them together with a library called Inertia.
Let's talk about Inertia just for a second. Inertia is the greatest JavaScript library that you've never heard of. It sits-- It sits in the middle of your stack, and it covers that network chasm. It sits between Laravel and React or Vue, and it binds them together in such a way that you can write fully featured JavaScript heavy, JavaScript only front ends and still use Laravel as the back end.
And you don't have to write API endpoints. You don't have to worry about client-side routing. All of your routing is still handled on the Laravel layer, but Inertia intercepts that and makes it feel like a fully single-page application. And everything that you write on the front end is in either React or Vue, depending on which one you pick. On the back end, Inertia has adapters for Laravel, Rails, Adonis, Phoenix, Symfony, Django, WordPress, and a whole bunch more.
On the front end, Inertia has adapters for React, Vue two and three, and Svelte. Routes, like I said, are still defined in your back-end framework of choice, and your front end is written in your front-end framework of choice. Inertia has affordances for stuff like partial reloads and static site generation and form requests and validation and a whole lot more.
When you combine Laravel and React using Inertia in the middle, not only does the network chasm go away, but the boundaries between your back end and your front end become crystal clear. And you get to keep the whole thing in one-- in one repo. It's still one application, and so you don't have to worry about keeping two different repos in sync. So you get to use React to build fully client-side, feature-rich interactivity without having to piece together your own back-end framework out of Just totally
random packages. And if you want end-to-end type safety, I know how much JavaScript people love type safety. If you want end-to-end type safety, you, you can do that. You can do that with Laravel and Inertia, and you can have fully type safe applications. I'll leave a few links below for more about that. What if you wanted to use Next.js?
You could use Next.js with Laravel. In fact, there is a starter kit that has Laravel and Next.js together. It will set up a Laravel API with authentication provided by Laravel Sanctum, and it gives you an authentication hook to make your life easier on the front end and abstract away a lot of that pain. So if you love Next.js, that's great.
Keep using it. I'm happy for you. What I'm asking is that you would consider pairing it with a powerful back end, like Laravel or Rails or Django or even Adonis, something that makes your life easy on the back end while still getting everything you want on the front end. I think you might find that using plain React plus Inertia and one of these back ends is gonna be a lot easier.
It might make your life a lot better, but whatever you wanna use, there's a pretty good chance Laravel has a first party maintained starter kit to get you on your way. I would be remiss if I didn't talk about Laravel Livewire. Laravel Livewire is a library to create rich client side interaction just using Laravel and PHP.
Of course, under the hood it uses JavaScript, but this is very similar to something like LiveView in Phoenix or Hotwire or even HTMX. It ends up looking a lot like React when you use the functional syntax. You can use a more traditional class-based syntax that may look more familiar if you're a PHP developer, but Livewire covers the network chasm so that you, you never have to write controllers or API endpoints.
You just write components, and Livewire will handle making sure the right component on the back end is linked to the front end. And Livewire was written by the same guy, Caleb Porzio, who wrote Alpine.js, and so those two work really, really nicely together. They're designed to be used together. Whatever you end up using, please know that Laravel can be used on the back end for that.
There's no reason to have an anemic underpowered back end just so you can have a really powerful front end. They work together. So whatever you wanna use, please save yourself the time and the frustration and pick one of these battle-proven technologies. If you wanna learn more about Laravel, I have a video here that I will show you. Until the next time, see ya.