Uploading a Laravel and React Project to Heroku

I am a full-stack web developer that enjoys coding in Laravel and React.
Search for a command to run...

I am a full-stack web developer that enjoys coding in Laravel and React.
No comments yet. Be the first to comment.
Here I will post some articles that help devs with simple and complex topics in Laravel.
Using Custom Console Commands
Switching stacks

If you don't know what Twin Macro is, check it out! It's a great library that blends Tailwind CSS and Styled Component systems, like Emotion. I love using Twin because it lets me use Tailwind while separating my styling from my markup and methods. If...

Many developers know that full-stack apps with a Vue frontend can be quickly spun up with Laravel Jetstream. What many don’t know is that recently, the Laravel team made it easy to make an Inertia app with Laravel Breeze. In this article, we'll make ...

Extending the Rule Object with a DateTime Trait

Using Custom Console Commands

I've been gone for a while, but I'm back!
During my 'break' I gave Inertia with Laravel another chance and ended up liking it. I'm still not a fan of how Laravel devs still have to go to multiple places for docs, but it's what we have so far until the Laravel team or someone brave puts everything in one place. When I was getting reacquainted, I needed to upload my project to production. I'm a frequent user of Heroku because of their free plan, but getting PHP and Node to work together took a bit of work. Here's how I did it.
Pre-requisites:
$ laravel new laravel-inertia-project in your terminal.First Steps: In this tutorial, we'll be using Laravel Breeze, which is the simplest way to implement a Vue or React frontend in our Laravel project.
$ composer require laravel/breeze --dev. Then we'll install Breeze and its Inertia stack by running these two commands:$ php artisan breeze:install --inertia
$ npm install
Once that's done, we'll need to set up React. Laravel Breeze and Inertia will automatically install Vue, so we'll need to do a bit of work here.
Run:
$ npm install react react-dom
$ npm install @inertiajs/inertia @inertiajs/inertia-react
Go to resources/js/app.js and make some changes so that we can build our own frontend instead of using Vue.
import {InertiaApp} from ‘@inertiajs/inertia-react’
import React from ‘react’
import {render} from ‘react-dom’
const app = document.getElementById(‘app’)
render (
<InertiaApp
initialPage={JSON.parse(app.dataset.page)}
resolveComponent={name => import(`./Pages/${name}`).then(
module => module.default
)}
/>,
app
)
$ npm install --save-dev @babel/preset-react. We can then use this preset by creating a .babelrc file in the root with this code:{
"presets": ["@babel/preset-react"]
}
import React from 'react'
const HelloWorld = () => (
<p>Hello world!</p>
)
export default HelloWorld
Route::get('/', function(){
return Inertia::render('HelloWorld', []);
}
mix.js('resources/js/app.js', 'public/js'. If you see .vue() attached to mix, delete it or you'll get errors. Now compile your assets by running $ npm run dev.The Work Begins:
$ git init to set git up in your project. Then add your files to git by running $ git add ..$ heroku create. web: vendor/bin/heroku-php-apache2 public/. Create Procfile in your root folder and copy-paste the line in the file, or run $ echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile in your terminal.
NOTE: It's best to create the file first before writing to it. You'll see less errors this way.The Work Continues:
$ heroku buildpacks:set heroku/php.$ heroku buildpacks:add --index 1 heroku/nodejs.--index 1 basically states that this buildpack will run first. The buildpack for the primary language must always be run last. devDependencies in package.json, which it does by default. Run $ heroku config:set NPM_CONFIG_PRODUCTION=false. The Work Ends:
$ git commit -m "First commit to Heroku"
$ git push heroku main
NOTE: If for some reason the heroku remote wasn't added, run $ heroku git:remote -a {APP_NAME}. Go to your Heroku dashboard to see the name if you lost it.
For Laravel to work, we'll need an APP_KEY. One is already set locally, but we need one for Heroku. Run $ heroku config:set APP_KEY={key}.
$ heroku open to open and run the app. It should open to https://{APP_NAME}.netlify.app and you should see "Hello World" on your screen.Conclusion:
$ heroku config:set LOG_CHANNEL=errorlog so your stack trace errors are logged there.