第二章 表和模型
先要确定我们都需要哪些表:
- Contacts table: 保存联系人和客户的数据
- Tasks table: 这里是任务,实际上这个任务有很多种类,包括客户需求、系统触发、条件触发等多种。
- Documents table: 文档表,这里是各种文档
- Mailbox table: 这个属于内部通信用的表
-
Settings table: 系统设置,针对于CRM系统的一些设定。
我们来看看主要的表的结构图:
这是联系人表。
联系人电话和邮件,这里单独列出表是因为一个人可能有多个电话或者邮件。
联系人的文档和状态。
任务表、任务状态表,任务类型表,任务和文档关联表。
文档和文档类型
系统设置表。
以上这些表是一个CRM系统中最基本的表的结构图,当然还应该有用户表,用户权限表等,幸运的是 laravel-admin 内置了非常好的权限控制,可以让我们很容易的处理用户和权限的问题。
接下来就是建立这些表
php artisan make:migration create_setting_table
php artisan make:migration create_document_type_table
php artisan make:migration create_document_table
php artisan make:migration create_task_status_table
php artisan make:migration create_task_type_table
php artisan make:migration create_contact_status_table
php artisan make:migration create_contact_table
php artisan make:migration create_contact_phone_table
php artisan make:migration create_contact_email_table
php artisan make:migration create_contact_document_table
php artisan make:migration create_task_table
php artisan make:migration create_task_document_table
接下来我们开始通过seed文件建立相应的数据表;
首先是设定表,通过上面的语句,这个文件应该已经被自动建立了,文件会在
database/migrations/XXXX_XX_XX_XXXXX_create_setting_table.php。后面
increments('id');
$table->string('setting_key')->unique();
$table->text('setting_value')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('setting');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_document_type_table.php
increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('document_type');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_document_table.php
increments('id');
$table->string('name');
$table->string('file');
$table->tinyInteger('status')->default(1)->comment('1=active 2=not active');
$table->integer('type')->unsigned()->nullable();
$table->string('publish_date')->nullable();
$table->string('expiration_date')->nullable();
$table->integer('created_by_id')->unsigned();
$table->integer('modified_by_id')->unsigned()->nullable();
$table->integer('assigned_user_id')->unsigned()->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('type')->references('id')->on('document_type')->onDelete('set null');
$table->foreign('created_by_id')->references('id')->on('users');
$table->foreign('modified_by_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('assigned_user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('document');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_task_status_table.php
increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('task_status');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_task_type_table.php
increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('task_type');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_contact_status_table.php
increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact_status');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_contact_table.php
increments('id');
$table->string('first_name');
$table->string('middle_name')->nullable();
$table->string('last_name');
$table->integer('status');
$table->string('referral_source')->nullable();
$table->string('position_title')->nullable();
$table->string('industry')->nullable();
$table->string('project_type')->nullable();
$table->string('company')->nullable();
$table->text('project_description')->nullable();
$table->text('description')->nullable();
$table->string('budget')->nullable();
$table->string('website')->nullable();
$table->string('linkedin')->nullable();
$table->string('address_street')->nullable();
$table->string('address_city')->nullable();
$table->string('address_state')->nullable();
$table->string('address_country')->nullable();
$table->string('address_zipcode')->nullable();
$table->integer('created_by_id')->unsigned();
$table->integer('modified_by_id')->unsigned()->nullable();
$table->integer('assigned_user_id')->unsigned()->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('created_by_id')->references('id')->on('users');
$table->foreign('modified_by_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('assigned_user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_contact_phone_table.php
increments('id');
$table->string('phone');
$table->integer('contact_id')->unsigned();
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contact');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact_phone');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_contact_email_table.php
increments('id');
$table->string('email');
$table->integer('contact_id')->unsigned();
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contact');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact_email');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_contact_document_table.php
increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('document_id')->unsigned();
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contact');
$table->foreign('document_id')->references('id')->on('document');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact_document');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_task_table.php
increments('id');
$table->string('name');
$table->string('priority')->nullable()->comment('Low Normal High Urgent');
$table->integer('status')->unsigned()->nullable();
$table->integer('type_id')->unsigned();
$table->string('start_date')->nullable();
$table->string('end_date')->nullable();
$table->string('complete_date')->nullable();
$table->string('contact_type')->nullable()->comment('Lead Opportunity Customer Close');
$table->integer('contact_id')->unsigned()->nullable();
$table->text('description')->nullable();
$table->integer('created_by_id')->unsigned();
$table->integer('modified_by_id')->unsigned()->nullable();
$table->integer('assigned_user_id')->unsigned()->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('status')->references('id')->on('task_status')->onDelete('set null');
$table->foreign('type_id')->references('id')->on('task_type');
$table->foreign('contact_id')->references('id')->on('contact')->onUpdate('set null')->onDelete('set null');
$table->foreign('created_by_id')->references('id')->on('users');
$table->foreign('modified_by_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('assigned_user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('task');
}
}
database/migrations/XXXX_XX_XX_XXXXX_create_task_document_table.php
increments('id');
$table->integer('task_id')->unsigned();
$table->integer('document_id')->unsigned();
$table->timestamps();
$table->foreign('task_id')->references('id')->on('task');
$table->foreign('document_id')->references('id')->on('document');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('task_document');
}
}
目前暂时先用这些表,当然随着功能的增加,还会增加新的表。
php artisan migrate
执行上面的命令,如果没什么错,那么恭喜你,数据库我们就算建完了。接下来的任务是往数据表里填充一些基本数据。
填充基础数据
现在我们需要用一些基础数据或者默认值填充我们的表。这个步骤也是必不可少的,因为这些数据决定了项目启动时的基本功能,首先是基本的配置文件
在 config/ directory 建立一个新的文件:seed_data.php。 修改代码:
['Contract', 'License Agreement', 'EULA', 'Other'],
'task_statuses' => ['Not Started', 'Started', 'Completed', 'Cancelled'],
'task_types' => ['Task', 'Meeting', 'Phone call'],
'contact_status' => ['Lead', 'Opportunity', 'Customer', 'Close'],
'settings' => ['crm_email' => '[email protected]', 'enable_email_notification' => 1],
'permissions' => [
'create_contact', 'edit_contact', 'delete_contact', 'list_contacts', 'view_contact', 'assign_contact',
'create_document', 'edit_document', 'delete_document', 'list_documents', 'view_document', 'assign_document',
'create_task', 'edit_task', 'delete_task', 'list_tasks', 'view_task', 'assign_task', 'update_task_status',
'edit_profile', 'compose_email', 'list_emails', 'view_email', 'toggle_important_email', 'trash_email', 'send_email',
'reply_email', 'forward_email', 'show_email_notifications', 'show_calendar'
],
'mailbox_folders' => array(
array("title"=>"Inbox", "icon" => "fa fa-inbox"),
array("title"=>"Sent", "icon" => "fa fa-envelope-o"),
array("title"=>"Drafts", "icon" => "fa fa-file-text-o"),
array("title"=>"Trash", "icon" => "fa fa-trash-o")
)
];
然后是database/seeds/DatabaseSeeder.php 文件,修改成这样
call(UsersTableSeeder::class);
// inserting document types
foreach (config('seed_data.document_types') as $value) {
DB::table('document_type')->insert([
'name' => $value
]);
}
// insert task status
foreach (config('seed_data.task_statuses') as $value) {
DB::table('task_status')->insert([
'name' => $value
]);
}
// insert task types
foreach (config('seed_data.task_types') as $value) {
DB::table('task_type')->insert([
'name' => $value
]);
}
// insert contact status
foreach (config('seed_data.contact_status') as $value) {
DB::table('contact_status')->insert([
'name' => $value
]);
}
// insert the system main email into settings table
foreach (config('seed_data.settings') as $key => $value) {
DB::table('setting')->insert([
'setting_key' => $key,
'setting_value' => $value
]);
}
}
}
最后执行
php artisan db:seed
然后检查一下数据是不是正常填充了。