/Laravel-learn

clone https://github.com/edomaru/laravel-contact-app

Primary LanguagePHP

Laravel learn

Environment

  • OS: Windows 10 Pro - 21H2

  • Nodejs - v18.15.0

  • Composer version 2.5.5 2023-03-21 11:50:05

  • Xampp version 8.0.25

  • PHP 8.0.25 (cli) (built: Oct 25 2022 10:49:29) ( ZTS Visual C++ 2019 x64 )

  • Laravel Installer 4.5.0

  • Mysql Ver 15.1 Distrib 10.4.27-MariaDB, for Win64 (AMD64),

  • Docker image

Install laravel project

  1. Download Laravel Installer using Composer

    composer global require laravel/installer 
  2. Create project

    composer create-project --prefer-dist laravel/laravel <project_name>
    # or
    cd <project_dir>
    laravel new <project_name>
    php artisan storage:link
    php artisan migrate
    php artisan migrate:fresh --seed
    php artisan migrate:refresh --seed
  3. Once u're in Laravel proj, fire up the Laravel Dev Server to make app run by:

    composer update
    composer install
    composer install --ignore-platform-reqs
    php artisan key:generate
    
    npm install
    npm run dev
    
    # Migrate database for system
    php artisan migrate --seed
    
    # Run seeder to create default for system
    php artisan db:seed --class=ContactSeeder
    php artisan db:seed
    php artisan serve 
    php artisan serve --port=8000
    php artisan serve --port=8000 --host=0.0.0.0
  4. Setup a Laravel project

    • Install Composer Dependencies

      composer install
    • Install NPM Dependencies

      npm install
    • Generate JS, CSS, image for system

      npm run dev
    • Create a copy of your .env file

      cp .env.example .env
    • Generate an app encryption key

      php artisan key:generate
    • In the .env file, add database information to allow Laravel to connect to the database

      In the .env file fill in the DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD options to match the credentials of the database you just created. This will allow us to run migrations and seed the database in the next step.

    • Migrate the database

      php artisan migrate
    • Seed the database

      php artisan db:seed
    • Recompile blade template

      php artisan view:cache
  5. Laravel project structure

    • app dir: place the business logic of app, Laravel follow MVC pattern -> M, and C part are placed in this dir.

    • config dir: put the configuration of app.

    • database dir: some files working with DB, e.g: defining table schema, defining fake data.

    • lang dir: language files for inernationalization.

    • public dir: stores files that can be accessed publicly, you can put you images, CSS and JS files inside.

    • resources dir: contains views or files used to build app interface. -> V part of MVC is stores in views folder. this dir cũng chứa code JS, CSS đã compiled mà KHÔNG truy cập trực tiếp từ browser

    • roots dir: contains all the root definition for app

    • storage dir: contains logs, compile blade templates, file based session, file caches, and other file gen by framwork.

    • test dir: contains files for automated test.

    • vendor dir: all dependencies installed by the composer.

    • .env file: contains configs for env

  6. Tinker

    php artisan tinker
  7. List all of the roots that are defined by app

    php artisan route:list
    php artisan route:list --except-vendor
    php artisan route:list --only-vendor
    php artisan route:list --path=contacts
    php artisan route:list --path=contacts -

Database

  1. CREATE DATABASE - create a new SQL database

    CREATE DATABASE contact_app;
  2. SHOW DATABASE - list all exist database

    SHOW DATABASE;
  3. DESCRIBE - show table's information

    DESCRIBE migrations
  4. ALTER DATABASE - change the characteristics of a database

    ALTER DATABASE database_name [COLLATE collation_name]
  5. USE - select a database to work with

    USE contact_app;
  6. CREATE TABLE - create a new table in the database

    CREATE TABLE table_name (
        column_1 datatype,
        column_2 datatype,
        column_3 datatype
    );
  7. SHOW TABLES - list all exist tables in database

    SHOW TABLES;
  8. ALTER TABLE - add, delete, or modify columns in an existing table.

    ALTER TABLE table_name ADD column_name datatype;
  9. DROP TABLE - delete an existing table in a database

    DROP TABLE table_name;
  10. SELECT - fetch data from a database

    SELECT column_name FROM table_name;
  11. UPDATE - edit rows in a table

    UPDATE table_name SET some_column = some_value WHERE some_column = some_value;
  12. DELETE - remove rows from a table

    DELETE FROM table_name WHERE some_column = some_value;
  13. INSERT INTO - add a new row to a table

    INSERT INTO table_name (column_1, column_2, column_3) VALUES (value_1, ‘value_2’, value_3);
  14. CREATE INDEX - create an index

    CREATE INDEX index_name ON table_name (column_name1, column_name2…);
  15. DROP INDEX - delete an index

    ALTER TABLE table_name DROP INDEX index_name;

Setup fake SMTP server

https://mailtrap.io/

  1. Get credentials
Email Testing > Inboxes > (select a Inbox) > SMTP Setting > Integrations (Laravel 7+) > Copy
  1. Config project
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Note

  • routes\web.php: define routes.
  • resources\views: V part in MVC pattern.