GVKun编程网logo

laravel创建数据表(使用命令行结合代码)(laravel 创建数据表)

18

本文将带您了解关于laravel创建数据表的新内容,同时我们还将为您解释使用命令行结合代码的相关知识,另外,我们还将为您提供关于3)-MySQL创建数据表、elasticsearch的单节点和分布式的

本文将带您了解关于laravel创建数据表的新内容,同时我们还将为您解释使用命令行结合代码的相关知识,另外,我们还将为您提供关于3)-MySQL创建数据表、elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)、lararvel 创建数据表、laravel migrate创建数据表的实用信息。

本文目录一览:

laravel创建数据表(使用命令行结合代码)(laravel 创建数据表)

laravel创建数据表(使用命令行结合代码)(laravel 创建数据表)

下面由Laravel框架教程栏目给大家介绍laravel 创建数据表,希望对需要的朋友有所帮助!

虽然可以直接在数据库中创建数据表,但是不便于以后项目的迁移。现使用命令行结合代码的方式来进行生成。

1、通过命令创建数据表文件

PHP artisan make:migration create_table_customers

laravel 创建数据表

2、在数据表文件中完善数据表相关字段

<?PHP

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

class CreateTableCustomers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('mobile')->nullable()->unique();
            $table->string('email')->unique();
            $table->string('website')->default('website')->comment('站点:applet、website');
            $table->string('store_id')->default('1')->comment('店铺 ID');
            $table->string('first_name');
            $table->string('last_name');
            $table->integer('appellation')->comment('称谓');
            $table->dateTime('birthday')->comment('生日');
            $table->string('province')->comment('省');
            $table->string('city')->comment('市');
            $table->string('district')->comment('区/县');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customers');
    }
}

laravel 创建数据表

3、生成数据表

PHP artisan migrate

laravel 创建数据表

laravel 创建数据表


此时,数据表已经生成!

3)-MySQL创建数据表

3)-MySQL创建数据表

MySQL 创建数据表

创建MySQL数据表需要以下信息:
  • 表名
  • 表字段名
  • 定义每个表字段

语法

以下为创建MySQL数据表的SQL通用语法:
 
create table table_name (column_name column_type);
 
以下例子中我们将在 RUNOOB 数据库中创建数据表runoob_tbl:
 
create table if not exists `runoob_tbl`(
   `runoob_id` int unsigned auto_increment,
   `runoob_title` varchar(100) not null,
   `runoob_author` varchar(40) not null,
   `submission_date` date,
   primary key ( `runoob_id` )
)engine=innodb default charset=utf8;
 
实例解析:
  • 如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。
  • AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
  • PRIMARY KEY关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。
  • ENGINE 设置存储引擎,CHARSET 设置编码。
通过命令提示符创建表
通过 mysql> 命令窗口可以很简单的创建MySQL数据表。你可以使用 SQL 语句 CREATE TABLE 来创建数据表。
 
root@host# mysql -u root -p
enter password:*******
mysql> use runoob;
database changed
mysql> create table runoob_tbl(
   -> runoob_id int not null auto_increment,
   -> runoob_title varchar(100) not null,
   -> runoob_author varchar(40) not null,
   -> submission_date date,
   -> primary key ( runoob_id )
   -> )engine=innodb default charset=utf8;
query ok, 0 rows affected (0.16 sec)
mysql>

elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)

elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)

关于安装是在另一个博客上面写的 不知道怎么同步 有兴趣的可以点击链接去看下

博客地址:elasticsearch单节点和分布式的安装

在文章开始之前、先明确三个概念 1、索引
2、类型 3、文档 对比于数据库中,索引就是一个数据库、类型就是数据库中的某张表、文档也就是表中具体的记录。抽象点来看,索引抽象成一个人、人又分为男人和女人(就是类型)、然后男人有姓名、年龄、身高等(就是文档)。

使用postman进行操作

	向es(elasticsearch,下面全部用es简称)发送操作的请求是一个RestFui风格的,类似下面这种:
		http://<ip>:<port>/<索引>/<类型>/<文档id>
下面使用post向es发送请求进行索引的创建

请求url:127.0.0.1:9200/people
请求入参:

{
    "settings": { 
    	"index.number_of_shards":3,
    	"index.number_of_replicas":1
    },
    "mappings": {
        "man": { 
        	"properties":{
        		"name":{
        			"type":"text"
        			
        		},
        		"height":{
        			"type":"text"
        			
        		},
        		"age":{
        			"type":"integer"
        			
        		}
        	}
        }
        
    }
}
上述的过程就是创建了一个索引为people、类型为man、类型中的properties有name、height、age这个字段

响应参数:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

插入数据有两种方式: 第一种 指定id来插入: 请求url:127.0.0.1:9200/people/man/1 (1就是我们指定的id) 请求入参json串:

{
	"name":"shuaige",
	"age" : 18,
	"height": "188cm"
}

响应:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

自动生成id: 请求url:127.0.0.1:9200/people/man 请求入参:

{
	"name":"laoshiren",
	"age" : 30,
	"height": "166cm"
}

响应参数:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWXxFS1S66ULpPmE4hFv",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

此时就创建了一个id为AWXxFS1S66ULpPmE4hFv的数据,其它的操作同理,都是像es发请求,只不过入参改改就可以

使用java代码进行操作

1、首先、构建一个springboot''工程、引入es的依赖
		<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>

新建一个配置文件的类,初始化一个es的客户端

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public TransportClient client() throws Exception {
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp端口是9300

        Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        return client;
    }
}

最后在一个controller里面对es进行操作

首先注入初始化es客户端:

	@Autowired
    private TransportClient client;

插入数据:

public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){

        try {
            XContentBuilder content = XContentFactory.jsonBuilder().startObject()
                    .field("name",name)
                    .field("height",height)
                    .field("age",age)
                    .endObject();
            IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get();
            log.info("插入数据成功={}",response);
            return new ResponseEntity(response.getId(),HttpStatus.OK);

        }catch (Exception e){
            log.error("插入数据异常={}", e);
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

最后为方法配置路由去访问就可以,其它的操作同理的。 代码的地址:代码下载连接

lararvel 创建数据表

lararvel 创建数据表

laravel migrate创建数据表

laravel migrate创建数据表

1,使用 Artisan 命令 make:migration 就可以创建一个新的迁移

PHP artisan make:migration create_users_table

迁移类包含了两个方法:up 和 downup 方法用于新增表,列或者索引到数据库,而 down 方法就是 up 方法的逆操作,和 up 里的操作相反。

<?PHP

 

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

 

class CreateFlightsTable extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::create('flights', function (Blueprint $table) {

            $table->increments('id');

            $table->string('name');

            $table->string('airline');

            $table->timestamps();

        });

    }

 

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        Schema::drop('flights');

    }

 

}

PHP artisan migrate

回滚迁移

PHP artisan migrate:rollback

删除所有表 & 迁移

PHP artisan migrate:fresh

文章来自 www.sxlenovo.com

使用 Artisan 命令 make:migration 就可以创建一个新的迁移

今天关于laravel创建数据表使用命令行结合代码的分享就到这里,希望大家有所收获,若想了解更多关于3)-MySQL创建数据表、elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)、lararvel 创建数据表、laravel migrate创建数据表等相关知识,可以在本站进行查询。

本文标签: