Skip to content

Instantly share code, notes, and snippets.

@BurningDog
Last active June 21, 2021 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurningDog/2eea2e60440b96dd419f1b588e58b0e1 to your computer and use it in GitHub Desktop.
Save BurningDog/2eea2e60440b96dd419f1b588e58b0e1 to your computer and use it in GitHub Desktop.
Setup for running your first Laravel site

This assumes that you have never run Laravel on your machine. If you're on Windows, stop reading this and install Homestead - it provisions a virtual machine with all of the necessary software installed.

  • Install homebrew - https://brew.sh/
  • Install composer - https://getcomposer.org/download/
  • Make sure you can open a terminal and run composer. If not, make sure it's linked to your path. If you don't know what that means, follow the next step and ask me for help!
  • brew install tmate so I can ssh into your laptop occasionally to debug and help
  • Install php - brew install php
  • Use a specific version of php (if necessary) - e.g. php 7.4 - valet use php@7.4
  • Install valet - composer global require laravel/valet
  • Set up valet: valet install
  • Download and install DBngin for easy database management
  • After DBngin has been installed, you can connect to your database at 127.0.0.1 using the root username and an empty string for the password.

Site setup

  • Open a terminal
  • Clone the Laravel repo you want to work with.
  • Change directory so that you're inside the Laravel folder
  • Install php dependencies - composer install
  • Install nodejs dependencies - npm install
  • Link the website with valet - valet link
  • Secure the website with valet - valet secure
  • Open the website in your browser - valet open - and accept the security warning (it's on a .test domain with a local certificate issuer - so it's fine for local testing)

This should open something like https://my-new-laravel-repo.test in your browser and show you a Laravel welcome page (if it's a blank Laravel repo).

Database setup

  • With DBngin create a new database
  • Create a local environment file: cp .env.example .env
  • Edit the .env file and put in your database credentials into this section:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_project_name
DB_USERNAME=root
DB_PASSWORD=

This assumes that the name of the database you created is laravel_project_name. It's bad practice to connect to the database with the root user, and even worse practice to not have a password. If you want to, fix this by creating a user and a password. Inside DBngin figure out how to run a query, and run:

CREATE USER laravel_user@'localhost';
ALTER USER laravel_user@'localhost' IDENTIFIED BY 'laravel_project_name_silly_password';
GRANT ALL ON laravel_project_name.* TO `laravel_user`@`localhost`;
flush privileges;

Then adjust your .env file as follows:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_project_name
DB_USERNAME=laravel_user
DB_PASSWORD=laravel_project_name_silly_password

That's better! Now add those credentials to DBngin for future connections. Verify that you can connect to the laravel_project_name database using those credentials.

Migrations

Migrations are how Laravel adds and alters tables in the database. This is managed via artisan. So, run:

php artisan migrate

Laravel setup

Follow the instructions when the website loads. For instance, if this is a Laravel 8 site it's going to ask you to run php artisan key:generate

Artisan is Laravel's command line helper. You run artisan commands in your terminal. Run that one!

Frontend dev

Then set up frontend JS/CSS by running npm run dev.

If you're doing frontend dev you can also run npm run watch and it will re-compile your JS and CSS any time a file in /resources/js or /resources/sass changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment