Skip to content

robertocannella/laravel-react-app

Repository files navigation

Things You Need on Your Machine

ADD DOCKER MYSQL SUPPORT

Link

Create the docker file

touch docker-compose.yml
echo 'version: "3.1"' >> docker-compose.yml
echo 'services:' >> docker-compose.yml

TO Persist data

Adds a volume directory (See Config Below)

echo '/data' >> .gitignore
mkdir data

Update .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8001 
DB_DATABASE=myapp
DB_USERNAME=myapp
DB_PASSWORD=myapp

Add mysql to docker compose:

version: "3.1"
services:
    mysql:
      image: mysql:8.0
      container_name: mysql
      volumes:
        - ./data/mysql:/var/lib/mysql
      environment:
        - MYSQL_ROOT_PASSWORD=myapp
        - MYSQL_DATABASE=larvel-react-app-db
        - MYSQL_USER=myapp
        - MYSQL_PASSWORD=myapp
      ports:
        - "8001:3306"

Configure DEFAUST DB Connection inside config/database.php

'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '8001'),
        'database' => env('DB_DATABASE', 'laravel-react-app-db'),
        'username' => env('DB_USERNAME', 'myapp'),
        'password' => env('DB_PASSWORD', 'myapp'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
]

Start Laravel Server

php artisan serve

Start docker container

 docker-compose up -d 

Test database connection through MYSQL SHell

https://dev.mysql.com/downloads/shell/

 sudo mysqlsh --sql -h localhost -P 8001 -u myapp -pmyapp -D myapp 

YOU MAY BE PROMPTED FOR DEVICE PASSWORD HERE

Run first migration

 php artisan migrate 

ADD REACT

Create a new @vite app (allows Laravel to communicate to FrontEnd Frameworkds)

npm create vite

  • Project name: … react
  • Select a framework: › React
  • Select a variant: › TypeScript + SWC

Now run

  cd react
  npm install
  npm run dev

If you'd like, you can change the port that the front end is running on via the react/package.json file

...

  "type": "module",
  "scripts": {
    "dev": "vite --port=3000",
    "build": "tsc && vite build",
    "preview": "vite preview"
  },
...
  

Add react router-dom

npm install react-router-dom -S

CSS AND STYLES

ADD SASS

https://www.youtube.com/watch?v=VaDZ4NS6dbY

npm add -D sass 
mkdir styles
touch styles/_main.css
echo "h1 {color: red;}" > styles/_main.css 

If styles are working, you are all set. Continue to install dev dependencies:

Add normalize.css

npm i normalize.css

Add import to _main.scss

/* _main.scss */

Add font awesome

npm i --save @fortawesome/fontawesome-svg-core
npm i --save @fortawesome/free-solid-svg-icons
npm i --save @fortawesome/free-regular-svg-icons
npm i --save @fortawesome/react-fontawesome@latest

Add import to _main.scss

@import "@fortawesome/fontawesome-free/css/all.css";

Add theme support https://javascript.plainenglish.io/building-a-custom-theme-provider-using-reacts-context-api-4e10de8eaf43] https://felixgerschau.com/react-typescript-context/

Add styled components

After installing styled-components, install @types for Typescript projects

npm install styled-components
npm i --save-dev @types/styled-components`

ADD AUTH CONTROLLER

php artisan make:controller Api/AuthController  

Add requests

php artisan make:request LoginRequest

php artisan make:request SignupRequest

Making models and Migrations:

This command makes a User model and creates a migration for that model.

  php artisan make:model Post -m 

From here you can edit the generated migration to add specific columns.

    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');    // maxes out at 255
            $table->text('excerpt');    // maxes out at 65,535
            $table->text('body');
            $table->timestamps();
            $table->timestamp('published_at')->nullable();
        });
    }

To apply the changes to the database, run the migrate command

php artisan migrate

Expose API

Build the nessessary files:

# Manages API endpint
php artisan make:controller Api/PostController --model=Post --resource --requests --api 


# Converts from database to JSON Serializable 
php artisan make:resource PostResource 

Add NEW Columns to table

php artisan make:migration add_first_and_last_to_users --table="users"

Original Laravel README.md

Laravel Logo

Build Status Total Downloads Latest Stable Version License

About Laravel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:

Laravel is accessible, powerful, and provides tools required for large, robust applications.

Learning Laravel

Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.

You may also try the Laravel Bootcamp, where you will be guided through building a modern Laravel application from scratch.

If you don't feel like reading, Laracasts can help. Laracasts contains over 2000 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.

Laravel Sponsors

We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Patreon page.

Premium Partners

Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.

License

The Laravel framework is open-sourced software licensed under the MIT license.

About

Complete Laravel React App

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published