美文网首页
Laravel Basic - 2 - 连接DB和使用model

Laravel Basic - 2 - 连接DB和使用model

作者: 芒鞋儿 | 来源:发表于2021-02-06 00:08 被阅读0次
  1. DB connection:
    .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=yourusername
DB_PASSWORD="youruserpasswd"

config/database.php:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'databasename'),
            'username' => env('DB_USERNAME', 'yourusername'),
            'password' => env('DB_PASSWORD', 'youruserpasswd'),
            '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'),
            ]) : [],
        ],

设定之后执行:

php artisan migrate

此时数据库中create 了三个基本表:migrations, users, password_resets;

在database/下创建了两个php 文件:
2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php

  1. 使用php artisan migrate 操作:create table and add fields
    创建products 表:
php artisan make:migration add_fields_to_products_table --table=products

refresh database/ 文件夹,可以看见多出了一个file: 2021_02_06_085510_create_products_table.php

php artisan make:migration add_fields_to_products_table --table=products

refresh database/ 文件夹,创建了另一个file:
2021_02_06_085510_create_products_table.php

在此file 中添加如下内容:

    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            //
            $table->string('name',100);
            $table->text('description');
            $table->string('image');
            $table->decimal('price',8,2);
            $table->string('type');

        });
    }

执行以下命令之后table中加入上述fields,

php artisan migrate

下面这个是用于回滚操作的:

    public function down()
    {
        Schema::table('products', function (Blueprint $table) {
            //
            $table->dropColumn('name');
            $table->dropColumn('description');
            $table->dropColumn('image');
            $table->dropColumn('price');
            $table->dropColumn('type');
        });
    }

执行以下命令可以回滚,删除fields

php artisan migrate:rollback

在表中插入数据需要直接sql 语句操作,当然也可以用phpadmin之类的tool

  1. 不用model, controller 直接操作DB
use Illuminate\Support\Facades\DB;
...
    public function index() {
        $products = DB::table('products')->get();
        return view("allproducts",compact("products"));
    }

allproducts.blade.php:

@foreach ($products as $product)
<p>{{ $product->name }}</p>
@endforeach
  1. 使用model:
    public function index() {
        $products = Product::all();  
        return view("allproducts",compact("products"));
    }

此处model 是通过命名规则和table名映射,但model是单数且头字母是大写,而table 是复数,头字母为小写,即:Product model 对应 products 表

model 很像OO设计中的展示层,可以在此将数据库抽取的数据进行一些加工再放到blade模板上

class Product extends Model
{
    //
    protected $fillable = [
        'name','description','image','price','type'
    ];
        
    public function getPriceAttribute($value) {
        $newForm = "$".$value;
        return $newForm;
    }

}

此处getPriceAttribute也是预定义命名规则下的function name
此时更新网页,可以看到price前已经加上了$符号。

products 列表

相关文章

网友评论

      本文标题:Laravel Basic - 2 - 连接DB和使用model

      本文链接:https://www.haomeiwen.com/subject/hyzotltx.html