我有一个数据库表teacher_attendances
,带字段:
id, name, data, attendance
。
我尝试基于teacher_attendances
表生成数据透视表。
我希望rowname
作为TeacherName
和columnname
作为Date
和cloumnname
低于标记出勤率。
如何在Laravel 4中创建数据透视表?
你检查过Laravel Migration和Schema Builder文档吗?
在CLI中运行php artisan migrate:make create_teacher_attendances_table --create --table=teacher_attendances
以生成文件。
然后编辑该文件以包含类似此方法的内容
public function up()
{
Schema::create('teacher_attendances', function(Blueprint $table)
{
$table->increments('id');
// Your code here
});
}
不要忘记foreign-keys。
您需要执行以下命令:
php artisan make:migration create_attendance_teacher --table="attendance_teacher"
注意:
create_attendance_teacher
分别以表格形式为表格和教师。 按照惯例尝试创建数据透视表字母顺序。因此我们在教师桌前拿了考勤表。
然后,在迁移脚本中:
public function up()
{
Schema::create('teacher_attendances', function(Blueprint $table)
{
$table->increments('id');
$table->integer('attendance_id');//Foreign key from attendances table
$table->increments('teacher_id');////Foreign key from teachers table
});
}