Laravel 怎样直接用模型方法创建一个对象

如题所述

1、使用php artisan make:model User_address命令创建模型,

2、成功之后再程序目录app和database/migrations下会分别生成两个文件,

3、打开database/migrations下生成的文件,这个文件就是控制生成数据库表的文件,内容如下:

2015_06_02_071328_create_user_addresses_table.php中的代码:

<?php

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

class CreateUserAddressesTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_addresses', function(Blueprint $table)
{
$table->increments('address_id')
->comment("主键");
$table->mediumInteger('user_id')
->comment('用户id');
$table->string('consignee', 60)
->comment('收货人');
$table->string('country', 60)
->comment('国家');
$table->string('province', 60)
->comment('省份');
$table->string('city', 60)
->comment('市');
$table->string('district', 120)
->comment('街道');
$table->string('address', 120)
->comment('详细地址');
$table->string('zip_code', 60)
->comment('政编码邮');
$table->string('tel', 60)
->comment('固定电话');
$table->string('mobile', 60)
->comment('手机');
$table->tinyInteger('is_default')
->comment('是否是默认地址');
});
}

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

}
4、执行:php artisan migrate 命令在数据库中生成表User_address。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-04
Laravel 怎样直接用模型方法创建一个对象
另外,现实世界中任何实体都可归属于某类事物,任何对象都是某一类事物的实例。
如果说传统的面向过程式编程语言是以过程为中心以算法为驱动的话,面向对象的编程语言则是以对象为中心以消息为驱动。
用公式表示,过程式编程语言为:程序=算法+数据;面向对象编程语言为:程序=对象+消息。本回答被网友采纳
相似回答