利用Laravel-Admin从头撸一个CRM--4

第四章:模型和关系

上一章我们一起建立了一个扩展,并且修改了路由,可以看到第一个画面了,接下来我们回到数据库,我们开始建立模型。利用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
好了,接下来我们完成每个模型的代码吧。

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

app/Models/ContactPhone.php

app/Models/ContactStatus.php

app/Models/Document.php

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

app/Models/Setting.php

app/Models/Task.php

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

app/Models/TaskStatus.php

app/Models/TaskType.php

app/User.php

 '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 框架解决认证的问题。

你可能感兴趣的:(利用Laravel-Admin从头撸一个CRM--4)