02 Laravel-Project Actual Combat Points Record

Summary of Laravel actual combat points

1. Development environment configuration

1.1 PHP operating environment

Commonly used PHP development integrated environment, choose one of them to install, please refer to the corresponding official website document for details of installation and configuration

  1. XAMPP: Integration of Apache + MariaDB + PHP + Perl
  2. WAMPSERVER: Integrate Apache + MySQL + PHP

1.2 Composer dependency management tool

A tool used by PHP to manage dependency relationships. You can declare the dependent external tool libraries (libraries) in your project, and Composer will help you install these dependent library files. Of course, it also integrates Laravel framework dependencies.

Different operating systems correspond to different operations corresponding to different installation methods, see official documents for details

1.2 Laravel operating environment

  1. Install the Laravel global operating environment through the Composer dependency management tool installed in the previous step, and execute it in the command window: composer global require laravel/installercommand line
  2. Configure Laravel's global environment variables:
  • macOS: $HOME/.composer/vendor/bin
  • Windows: %USERPROFILE%\AppData\Roaming\Composer\vendor\bin

2. Summary of main points

2.1 Laravel creates a template project

composer create-project --prefer-dist laravel/laravel blog

2.2 Laravel routing

Make different responses according to the URL entered by the front end

  1. app/Http/Controllers create controller
<?php

  namespace App\Http\Controllers;
	use Illuminate\Http\Request;

	class Home extends Controller
  {
    public function home() {
    	return view('home');
    }
}
Artisan command to create a controller:
php artisan make:controller Home
  1. routes/web.php create a route
<?php
use Illuminate\Support\Facades\Route;
use App\HttP\Controllers\Home; // 引入Home控制器
use App\HttP\Controllers\List;

Route::get('/',[Home::class, 'home']);
Route::get('/list/{lid}',[Details::class, 'details']);
Code description:
  1. Conventional routing identifier:'/'
  2. Route identifier with parameters:'/list/{ lid }'
  3. [Home::class,'home']:
  4. Home::class specifies the class name of the controller
  5. Methods of the'home' class

2.3 Connect to the database

  1. MySQL environment configuration for Wampserver integrated environment

Find the path of the bin file of the MySQL file in the Wampserver installation directory , and assign its absolute path to the Path configuration of its system environment. After this operation is completed, it can be easily accessed through the terminal of the editor

  1. .env Set configuration content
...其他配置
  
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dessert // 数据库名
DB_USERNAME=root // 用户名
DB_PASSWORD=123456 // 密码

...其他配置
  1. database/migrations Create data migration file
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCakesTable extends Migration
{
    public function up()
    {
      // 创建表
        Schema::create('cakes', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('type'); // 表的type字段
            $table->string('size'); // 表的size字段
            $table->string('name'); // 表的name字段
        });
    }

    public function down()
    {
        Schema::dropIfExists('cakes');
    }
}
Artisan command to create a controller:
php artisan make:migration create_cakes_table
  1. Synchronize the data in the data migration to the database
php artisan migration
  1. Add new fields to the created data migration table, and synchronize the database
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddPriceToCakesTable extends Migration
{
    public function up()
    {
      	// 指向需要添加新字段的表
        Schema::table('cakes', function (Blueprint $table) {
            $table->integer('price'); // 新字段price
        });
    }

    public function down()
    {
        Schema::table('cakes', function (Blueprint $table) {
            
        });
    }
}
php artisan migration
Artisan command to create a controller:
php artisan make:migration add_price_to_cakes_table
Top: The naming similar to'add_price_to_cakes_table' has relevant specifications

2.4 Create Eloquent Model

  1. app/Models Create Model files related to database interaction
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Cake extends Model
{
    use HasFactory;

    protected $table = 'cakes'; // 指定表
}
Artisan command to create a controller:
php artisan make:model Cake
  1. The related controller introduces the Model file, which can operate on the data table
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Cake; // 引入Model


class Cakes extends Controller
{
    public function cakes(Request  $request)
    {

        $cakeDate = Cake::all();// 获取Model指定表全部数据

        // 获取查询字符串
        $name = $request->name;

        return view('cakes',[
            'name' => $name,
            'cakeData' => $cakeDate,
        ]);
    }
}