第二章 表和模型
先要確定我們都需要哪些表:
- 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。后面
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSettingTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('setting', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDocumentTypeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('document_type', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDocumentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('document', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskStatusTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('task_status', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskTypeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('task_type', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactStatusTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact_status', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactPhoneTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact_phone', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactEmailTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact_email', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactDocumentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact_document', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskDocumentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('task_document', function (Blueprint $table) {
$table->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。 修改代碼:
<?php
// Database seeder data
return [
'document_types' => ['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' => 'noreply@mini-crm.com', '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 文件,修改成這樣
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// $this->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
然后檢查一下數據是不是正常填充了。