深入Laravel Nova教程(二)

在我的上一篇文章我简单的介绍了一下Nova的用法,下面我将通过新建一个简单的CRM系统来更深入的了解Nova

在此过程中,我们将了解如何使用Nova的 metrics , 自定义搜索条, 配置 filters 和 lenses,听起来很复杂,咱们一步一步开始吧!

本文代码:https://github.com/leienshu/learn-nova

开始

不会安装Nova的请参考我的上一篇写Nova的入门文章, 本文默认你已经安装好了,并且创建了一个登录用户。

访客提交

在我们深入了解Nova之前,我们需要创建Laravel模型。 我们的CRM将创建使用两个模型。

  • 一个Visitor模型,我们将用它来为我们跟踪与我们互动的所有访客。
  • 一个Note模型,我们的管理员用户可以为潜在客户留下笔记,访客也可以留下笔记。

因此,我们的模型关系,我们的潜在客户将拥有许多Notes,我们的管理员用户也将拥有许多Notes。

下面我们开始创建模型:

php artisan make:model Visitor -a
php artisan make:model Note -a

上面-a参数的意思是同时创建 Model、Migration、Controller、Factory 。

迁移

修改visitors的migration文件:

public function up()
    {
        Schema::create('visitors', function (Blueprint $table) {
            $table->increments('id');
            $table->text('type')->comment('类型');
            $table->text('status')->comment('状态');
            $table->text('name')->comment('名字');
            $table->text('email')->comment('邮箱');
            $table->timestamps();
        });
    }

修改notes的migration文件:

public function up()
    {
        Schema::create('notes', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->comment('用户ID');
            $table->integer('visitor_id')->comment('访客ID');
            $table->text('priority')->comment('优先级');
            $table->text('title')->comment('标题');
            $table->text('body')->comment('内容');
            $table->timestamps();
        });
    }

执行迁移:

php artisan migrate

模型

我们的每一条note既有访客又有管理用户,所以我们定义note的模型如下:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function visitor()
    {
        return $this->belongsTo('App\visitor');
    }
}

然后我们继续添加我们的User模型和Visitor模型和Note模型的关系:
都增加以下代码:

public function notes()
    {
        return $this->hasMany('App\Note');
    }

好了,我们继续在Visitor模型中添加一些fillable属性。

protected $fillable = [
        'type',
        'status',
        'email',
        'name'
    ];

最后,让我们在Visitor和Note模型中提供一些常量和辅助方法,以便我们可以轻松访问它们的类型,状态和优先级值。

// Note.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
    // 定义优先级
    const LOW_PRIORITY = 'Low';
    const MEDIUM_PRIORITY = 'Medium';
    const HIGH_PRIORITY = 'High';

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function visitor()
    {
        return $this->belongsTo('App\Visitor');
    }

    // 获取优先级
    public static function getPriorities()
    {
        return [
            self::LOW_PRIORITY => self::LOW_PRIORITY,
            self::MEDIUM_PRIORITY => self::MEDIUM_PRIORITY,
            self::HIGH_PRIORITY => self::HIGH_PRIORITY,
        ];
    }
}

// Visitor.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Visitor extends Model
{
    // 定义用户常量
    const ORGANIC_TYPE = 'Organic';  //基本类型
    const USER_SUBMITTED_TYPE = 'User Submitted';  //用户提交类型

    const PROSPECT_STATUS = 'Prospect';  //预期
    const VISITOR_STATUS = 'Visitor';  //访客状态
    const CUSTOMER_STATUS = 'Customer'; //客户状态

    protected $fillable = [
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值