利用Laravel-Admin从头撸一个CRM--4
2021-02-01 本文已影响0人
瞧俺老孙的
第四章:模型和关系
上一章我们一起建立了一个扩展,并且修改了路由,可以看到第一个画面了,接下来我们回到数据库,我们开始建立模型。利用Laravel 的 artisan 命令可以很方便的建立模型,在项目的根目录下执行:
php artisan make:model Contact
php artisan make:model ContactDocument
php artisan make:model ContactEmail
php artisan make:model ContactPhone
php artisan make:model ContactStatus
php artisan make:model Document
php artisan make:model DocumentType
php artisan make:model Setting
php artisan make:model Task
php artisan make:model TaskDocument
php artisan make:model TaskStatus
php artisan make:model TaskType
可能有聪明的小伙伴已经注意到我没有建立user 的表和user 的模型,因为laravel-admin 默认建立了一个user表,但是这个表的结构和我们设计的表的结构不一样,没关系,我们增加一些字段,然后修改已经有的user模型。
php artisan make:migration alter_users_table --table=users
然后执行
php artisan migrate
好了,接下来我们完成每个模型的代码吧。
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Contact extends Model
{
use SoftDeletes;
public $timestamps = true;
protected $table = "contact";
protected $fillable = ["first_name", "middle_name", "last_name", "status", "referral_source", "position_title", "industry",
"project_type", "company", "project_description", "description", "budget", "website", "linkedin",
"address_street", "address_city", "address_state", "address_country", "address_zipcode", "created_by_id",
"modified_by_id", "assigned_user_id"];
/**
* get created by user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function createdBy()
{
return $this->belongsTo(User::class, 'created_by_id');
}
/**
* get modified by user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function modifiedBy()
{
return $this->belongsTo(User::class, 'modified_by_id');
}
/**
* get assigned to user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function assignedTo()
{
return $this->belongsTo(User::class, 'assigned_user_id');
}
/**
* get status object of this contact i.e lead, opportunity, etc.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function getStatus()
{
return $this->belongsTo(ContactStatus::class, 'status');
}
/**
* get all documents for this contact (this relation is a many to many)
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function documents()
{
return $this->belongsToMany(Document::class, 'contact_document', 'contact_id', 'document_id');
}
/**
* get all emails for this contact
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function emails()
{
return $this->hasMany(ContactEmail::class, 'contact_id');
}
/**
* get all phones for this contact
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function phones()
{
return $this->hasMany(ContactPhone::class, 'contact_id');
}
/**
* get all tasks related to this contact
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tasks()
{
return $this->hasMany(Task::class, 'contact_id');
}
public function getName()
{
return $this->first_name . (!empty($this->middle_name)?" " . $this->middle_name . " ":"") . (!empty($this->last_name)?" " . $this->last_name:"");
}
}
app/Models/ContactDocument.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ContactDocument extends Model
{
protected $table = "contact_document";
protected $fillable = ["contact_id", "document_id"];
}
app/Models/ContactPhone.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ContactPhone extends Model
{
protected $table = "contact_phone";
protected $fillable = ["phone", "contact_id"];
}
app/Models/ContactStatus.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ContactStatus extends Model
{
protected $table = "contact_status";
protected $fillable = ["name"];
}
app/Models/Document.php
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Document extends Model
{
use SoftDeletes;
protected $table = "document";
public $timestamps = true;
protected $fillable = ["name", "file", "status", "type", "publish_date", "expiration_date", "created_by_id", "modified_by_id", "assigned_user_id"];
/**
* get created by user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function createdBy()
{
return $this->belongsTo(User::class, 'created_by_id');
}
/**
* get modified by user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function modifiedBy()
{
return $this->belongsTo(User::class, 'modified_by_id');
}
/**
* get assigned to user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function assignedTo()
{
return $this->belongsTo(User::class, 'assigned_user_id');
}
/**
* get type object for this document
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function getType()
{
return $this->belongsTo(DocumentType::class, 'type');
}
}
app/Models/DocumentType.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DocumentType extends Model
{
protected $table = "document_type";
protected $fillable = ["name"];
}
app/Models/Setting.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $table = "setting";
protected $fillable = ["setting_key", "setting_value"];
}
app/Models/Task.php
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Task extends Model
{
use SoftDeletes;
public $timestamps = true;
protected $table = "task";
protected $fillable = ["name", "priority", "status", "type_id", "start_date", "end_date", "complete_date", "contact_type", "contact_id", "description", "created_by_id", "modified_by_id", "assigned_user_id"];
/**
* get created by user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function createdBy()
{
return $this->belongsTo(User::class, 'created_by_id');
}
/**
* get modified by user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function modifiedBy()
{
return $this->belongsTo(User::class, 'modified_by_id');
}
/**
* get assigned to user object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function assignedTo()
{
return $this->belongsTo(User::class, 'assigned_user_id');
}
/**
* get status object for this task i.e completed, started, etc.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function getStatus()
{
return $this->belongsTo(TaskStatus::class, 'status');
}
/**
* get type object for this task
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function type()
{
return $this->belongsTo(TaskType::class, 'type_id');
}
/**
* get contact object attached with this task
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function contact()
{
return $this->belongsTo(Contact::class, 'contact_id');
}
/**
* get all documents for this task (this is a many to many relation)
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function documents()
{
return $this->belongsToMany(Document::class, 'task_document', 'task_id', 'document_id');
}
}
app/Models/TaskDocument.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TaskDocument extends Model
{
protected $table = "task_document";
protected $fillable = ["task_id", "document_id"];
}
app/Models/TaskStatus.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TaskStatus extends Model
{
protected $table = "task_status";
protected $fillable = ["name"];
}
app/Models/TaskType.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TaskType extends Model
{
protected $table = "task_type";
protected $fillable = ["name"];
}
app/User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'position_title', 'phone', 'image', 'is_admin', 'is_active'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* get all contacts assigned to user
*
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function contacts()
{
return $this->hasMany(Contact::class, 'assigned_user_id');
}
/**
* get all leads assigned to user
*/
public function leads()
{
return $this->hasMany(Contact::class, 'assigned_user_id')->where('status', ContactStatus::where('name', config('seed_data.contact_status')[0])->first()->id);
}
/**
* get all opportunities assigned to user
*/
public function opportunities()
{
return $this->hasMany(Contact::class, 'assigned_user_id')->where('status', ContactStatus::where('name', config('seed_data.contact_status')[1])->first()->id);
}
/**
* get all customers assigned to user
*/
public function customers()
{
return $this->hasMany(Contact::class, 'assigned_user_id')->where('status', ContactStatus::where('name', config('seed_data.contact_status')[2])->first()->id);
}
/**
* get all closed/archives customers assigned to user
*/
public function archives()
{
return $this->hasMany(Contact::class, 'assigned_user_id')->where('status', ContactStatus::where('name', config('seed_data.contact_status')[3])->first()->id);
}
/**
* get all documents assigned to user
*/
public function documents()
{
return $this->hasMany(Document::class, 'assigned_user_id');
}
/**
* get all tasks assigned to user
*/
public function tasks()
{
return $this->hasMany(Task::class, 'assigned_user_id');
}
/**
* get all completed tasks assigned to user
*/
public function completedTasks()
{
return $this->hasMany(Task::class, 'assigned_user_id')->where('status', TaskStatus::where('name', config('seed_data.task_statuses')[2])->first()->id);
}
/**
* get all pending tasks assigned to user
*/
public function pendingTasks()
{
return $this->hasMany(Task::class, 'assigned_user_id')->where('status', TaskStatus::whereIn('name', [config('seed_data.task_statuses')[0], config('seed_data.task_statuses')[1]])->first()->id);
}
}
请注意user模型,作为一个销售人员,他必须具有能查看联系人,上级,销售机会等,同时也能查看看自己的任务和任务的状态,所以我们添加了
User::contacts(), User::leads(), User::tasks, 等方法。
现在我们利用laravel-admin 框架解决认证的问题。