第二章 表和模型
先要確定我們都需要哪些表:
- Contacts table: 保存聯(lián)系人和客戶的數(shù)據(jù)
- Tasks table: 這里是任務(wù),實(shí)際上這個任務(wù)有很多種類诈茧,包括客戶需求、系統(tǒng)觸發(fā)捂掰、條件觸發(fā)等多種敢会。
- Documents table: 文檔表,這里是各種文檔
- Mailbox table: 這個屬于內(nèi)部通信用的表
-
Settings table: 系統(tǒng)設(shè)置尘颓,針對于CRM系統(tǒng)的一些設(shè)定走触。
我們來看看主要的表的結(jié)構(gòu)圖:
這是聯(lián)系人表。
聯(lián)系人電話和郵件疤苹,這里單獨(dú)列出表是因?yàn)橐粋€人可能有多個電話或者郵件互广。
聯(lián)系人的文檔和狀態(tài)。
任務(wù)表卧土、任務(wù)狀態(tài)表惫皱,任務(wù)類型表,任務(wù)和文檔關(guān)聯(lián)表尤莺。
文檔和文檔類型
系統(tǒng)設(shè)置表旅敷。
以上這些表是一個CRM系統(tǒng)中最基本的表的結(jié)構(gòu)圖,當(dāng)然還應(yīng)該有用戶表颤霎,用戶權(quán)限表等媳谁,幸運(yùn)的是 laravel-admin 內(nèi)置了非常好的權(quán)限控制,可以讓我們很容易的處理用戶和權(quán)限的問題晴音。
接下來就是建立這些表
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文件建立相應(yīng)的數(shù)據(jù)表锤躁;
首先是設(shè)定表系羞,通過上面的語句,這個文件應(yīng)該已經(jīng)被自動建立了椒振,文件會在
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');
}
}
目前暫時(shí)先用這些表,當(dāng)然隨著功能的增加洒缀,還會增加新的表。
php artisan migrate
執(zhí)行上面的命令萨脑,如果沒什么錯渤早,那么恭喜你鹊杖,數(shù)據(jù)庫我們就算建完了骂蓖。接下來的任務(wù)是往數(shù)據(jù)表里填充一些基本數(shù)據(jù)涯竟。
填充基礎(chǔ)數(shù)據(jù)
現(xiàn)在我們需要用一些基礎(chǔ)數(shù)據(jù)或者默認(rèn)值填充我們的表空厌。這個步驟也是必不可少的,因?yàn)檫@些數(shù)據(jù)決定了項(xiàng)目啟動時(shí)的基本功能筐钟,首先是基本的配置文件
在 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
]);
}
}
}
最后執(zhí)行
php artisan db:seed
然后檢查一下數(shù)據(jù)是不是正常填充了。