📚
note
Blog
  • Initial page
  • JS-notes
    • 使用浏览器书签执行js代码
    • JSON.stringify的小技巧
    • Fisher–Yates shuffle洗牌算法
    • 打印页面
  • Web-notes
    • 在网页中引入在线字体
  • Uniapp-notes
    • swiper-item高度自适应
    • 微信小程序的图片预览兼容性处理
  • VUE-notes
    • vue-note
    • vue3-note
  • VPS-notes
    • CentOs7笔记
    • ssh小记
    • Ubuntu笔记
    • vps安全相关
    • [Google Drive笔记](VPS-notes/Google Drive笔记.md)
  • TypeScript-notes
    • ts热编译项目
    • TypeScript笔记
    • js项目转ts
  • Python-notes
    • Python爬虫笔记
    • Python笔记
  • PHP-notes
    • php笔记
    • php+redis
    • php-codeIgniter
    • php抽奖算法
    • Laravel笔记
  • Mobile-notes
    • 移动端常用样式及兼容相关
  • Linux-notes
    • linux常用指令
  • Game-notes
    • Minecraft-server
  • TelegramBot-notes
    • tg-bot小记
  • Windows-notes
    • window-note
    • node-note
    • WSL-note
  • RaspberryPi-notes
    • RaspberryPi-note
    • 其他玩法
    • Openwrt
    • Ubuntu安装指南
  • Phone-notes
    • ZenFone6-note
  • Cocos-notes
    • Cocos-note
  • Network-notes
    • 单线复用
  • Other-notes
    • 国际化地域标识码
Powered by GitBook
On this page
  • 数据库初始化
  • 配置
  • 创建数据库表文件
  • 创建数据库表模型
  • 使用前端脚手架开发后端视图页面

Was this helpful?

  1. PHP-notes

Laravel笔记

Last updated 2 years ago

Was this helpful?

使用Laravel 6版本。

数据库初始化

配置

在/config/database.php里修改正确的数据库连接配置

创建数据库表文件

在Laravel项目内创建数据库文件:/database/migrations/,创建表的逻辑需要写在up方法内,需要注意每创建了一个表,需要在down方法内写一个删除表的方法(dropIfExists({name})),用于回滚。

创建数据库表模型

在数据库里取数据的时候,有一些敏感字段需要进行隐藏,这时可以在/app/Models/里创建对应表名的php文件(首字母大写),并在里面添加$hidden属性;在对数据库进行编辑/新增的时候,你还可以设置哪些字段允许更改,使用$fillable属性来实现。

<?php

namespace App\Models;

use Eloquent;

class Admin extends Eloquent
{
    protected $table = 'admin_list';
    /*
     * The attributes that are mass assignable.
     *
     * @var array
     */
    // 插入数据时可更改的字段
    protected $fillable = [
        'username', 'password'
    ];

    /*
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    // 取出数据时隐藏的字段
    protected $hidden = [
        'password'
    ];
}

准备工作完成后即可执行命令进行数据库迁移(创建数据库):php artisan migrate

使用前端脚手架开发后端视图页面

在安装完Laravel后,也许你想使用vue或者react开发视图页面, Laravel 提供的引导和 vue 脚手架位于 laravel/ui composer 包中,可以使用 composer 进行安装:

composer require laravel/ui --dev

安装完成后就可以生成对应的脚手架了

// 生成基本脚手架
php artisan ui vue
php artisan ui react

npm install

安装热更新调试模块

 npm install browser-sync browser-sync-webpack-plugin@2.0.1 --save-dev --production=false

在/webpack.mix.js中配置热更新

// 在 webpack.mix.js 中添加配置
mix.browserSync({
    proxy: 'localhost:8000'
});

执行前端视图热更新调试

npm run watch

启动Laravel

php artisan serve --host=localhost --port=3000

这时,/resources/views/welcome.blade.php 相当于vue脚手架里的app.vue,/resources/js/app.js相当于main.js

文档地址
文档地址