在前面的两篇文章里,我一步一步介绍了 Laravel Nova 的一些特性,这里我将针对第二篇文章新建的那个 CRM 做一些改进。

在本教程中,我们将了解如何使用 Nova 的操作(Action)和授权。

开始

如果您是 Nova 的新手,我建议您查看下我以前的文章,我详细解释了如何在您的机器上安装和设置 Nova。 在本教程中,我将假设我们在一个新的 Laravel 项目中安装了 Nova。 所以,你应该能够登录并看到下面这个图片:

优化一些CSS样式

上一篇文章的首页界面有点丑陋,我这里增加一点CSS样式,让他好看点。

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Nova CRM Demo</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">
    <!-- Styles -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet">
    <title>Nova CRM 演示</title>
</head>
<body>
    <div class="flex flex-col items-center justify-center md:h-screen md:bg-blue-lightest">
        <form class="w-full max-w-md bg-white rounded p-10 md:shadow" action="/form-submit" method="POST">
            <h1 class="text-grey-darkest mb-8 text-center">注册我们的Nova程序</h1>
            {{ csrf_field() }}
                <div class="flex flex-wrap -mx-3 mb-6">
                    <label  class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="name">姓名:</label>
                    <input class="appearance-none block w-full bg-grey-lighter text-grey-darker border rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white {{ $errors->has('name') ? 'border-red mb-3' : 'border-grey-lighter' }} id="name" name="name" type="text" placeholder="Lei">
                </div>

                <div>
                    <div class="flex flex-wrap -mx-3 mb-6">
                        <label class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="email">
                            邮箱
                        </label>
                        <input class="appearance-none block w-full bg-grey-lighter text-grey-darker border rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-grey {{ $errors->has('email') ? 'border-red mb-3' : 'border-grey-lighter' }}" id="email" name="email" type="email" placeholder="lei@example.com">
                        @if($errors->has('email'))
                            <p class="text-red text-xs italic">{{ $errors->first('email') }}</p>
                        @endif
                    </div>
                </div>
                <button class="block bg-blue px-6 py-4 text-white rounded mx-auto hover:bg-blue-dark" type="submit">提交</button>
            </form>
            @if (session('form-success'))
                <div class="bg-green mt-8 p-6 rounded shadow">
                    <p class="text-white">{{ session('form-success') }}</p>
                </div>
            @endif
        </div>
</body>
</html>
更改后的样式

Actions

对于我们的CRM,如果我们可以直接从索引页面更新我们的潜在客户状态,那就太棒了。 这样,我们的销售团队可以在他们打销售电话时快速管理潜在客户。

首先,我们将使用Nova cli生成器使用以下命令为我们启动Action。

php artisan nova:action UpdateVisitorStatus

找到app\Nova\Actions 文件夹下面的 UpdateVisitorStatus.php文件,你可以看到两个方法,一个handle 一个 fields 方法。

让我们首先定义我们的fields(),这样我们就可以在handle()中使用它们。 在我们的这个例子中,我们想询问用户他们希望访客拥有哪种状态。 为此,我们可以使用我们在Visitor Resource中使用的相同代码。

public function fields()
    {
        return [
            Select::make('Status')
            ->options(\App\Visitor::getStatuses()),
        ];
    }

注意引入:
use Laravel\Nova\Fields\Select;

现在,我们可以接收用户的状态选择,我们可以编写我们的handle()方法。

public function handle(ActionFields $fields, Collection $models)
    {
        foreach ($models as $model) {
            $model->status = $fields->status;
            $model->save();
        }
    }

到上面我们的Action就完成了,现在在Visitor Resource里面注册。

public function actions(Request $request)
    {
        return [
            new Actions\UpdateVisitorStatus,
        ];
    }
Action

如果你这里没有显示的话,请记住选中任意一条记录,你不选中是不会显示的。

另外:下面的两个参数可以控制Action显示在首页还是在详情页。

public $onlyOnIndex = true;
public $onlyOnDetail = true;
详情页的Action

授权

确保用户只能与他们有权访问的功能进行交互是一件大事。 在过去的项目中,这个处理起来很麻烦,不过幸运的是,Nova实现起来轻而易举。

我们的这个CRM,由于是我们团队使用的内部工具,因此我们没有大量的访问问题需要处理。 所以我们只要确保我们的管理员用户才能删除潜在客户,用户和标签。

首先,我们要给用户模型创建一个isAdmin的方法。

当然我这里是采用硬编码的方式来处理,你可以保存到数据库里,也可以用一个权限包来处理。

public function isAdmin()
    {
        $admin_emails = [
            'rennesusher@gmail.com'
        ];

        return in_array($this->email, $admin_emails);
    }

把上面的邮箱换成你的管理员邮箱。

对了,这里提一下,Nova是开箱即用,它有Laravel的policy功能,你不明白的话去翻文档吧。

php artisan make:policy UserPolicy -m User
php artisan make:policy VisitorPolicy -m Visitor 
php artisan make:policy NotePolicy -m Note

在 AuthServiceProvider.php 文件中注册。

protected $policies = [
        'App\User' => 'App\Policies\UserPolicy',
        'App\Visitor' => 'App\Policies\VisitorPolicy',
        'App\Note' => 'App\Policies\NotePolicy',
    ];

通过注册策略,我们可以运行所有预先生成的方法并定义我们的访问逻辑。

public function view(User $user, <model> $model)
{
    return true;
}

public function create(User $user)
{
    return true;
}

public function update(User $user, <model> $model)
{
    return true;
}

public function delete(User $user, <model> $model)
{
    return $user->isAdmin();
}

public function restore(User $user, <model> $model)
{
    return $user->isAdmin();
}

public function forceDelete(User $user, <model> $model)
{
    return $user->isAdmin();
}

注意上面的<model>对应的就是我们新建的3个policy。

换一个非管理员登录下nova后台,你可以看到删除什么按钮都消失了。

没有删除选项了

完结

好了,关于nova的三篇文章都结束了,欢迎提意见。

项目源代码地址:https://github.com/leienshu/learn-nova

提前祝大家新春快乐!

改进我们的Laravel Nova CRM(三)
Tagged on:     

21 thoughts on “改进我们的Laravel Nova CRM(三)

  • 2019-05-03 at 05:56
    Permalink

    With havin so much content and articles do you ever run into any problems of plagorism or copyright infringement?
    My site has a lot of completely unique content I’ve either written myself or
    outsourced but it appears a lot of it is popping it up all over
    the internet without my permission. Do you know any methods to help protect against content from
    being stolen? I’d genuinely appreciate it.

    Reply
  • 2019-05-03 at 20:34
    Permalink

    Hello, i think that i saw you visited my website so i came to “return the favor”.I am attempting to find things to improve my site!I
    suppose its ok to use some of your ideas!!

    Reply
  • 2019-05-04 at 08:21
    Permalink

    It’s enormous that you are getting ideas from this piece of writing as well
    as from our argument made here.

    Reply
  • 2019-05-05 at 16:05
    Permalink

    Hey there! Do you know if they make any plugins to safeguard against hackers?

    I’m kinda paranoid about losing everything I’ve worked hard on. Any
    suggestions?

    Reply
  • 2019-05-06 at 04:34
    Permalink

    Thanks designed for sharing such a good idea,
    post is pleasant, thats why i have read it completely

    Reply
  • 2019-05-08 at 00:06
    Permalink

    When I initially left a comment I seem to have clicked on the -Notify me
    when new comments are added- checkbox and from now
    on each time a comment is added I recieve 4 emails with
    the same comment. Is there an easy method you are able to remove me from that service?
    Cheers!

    Reply
  • 2019-05-09 at 12:33
    Permalink

    The other day, while I was at work, my sister stole my apple ipad and tested to see if it can survive a twenty five foot drop,
    just so she can be a youtube sensation. My apple ipad is now destroyed and she
    has 83 views. I know this is completely off topic but I had to share
    it with someone!

    Reply
  • 2019-05-09 at 22:19
    Permalink

    Pretty section of content. I just stumbled upon your site and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Any way I will be subscribing to your augment and even I achievement you access consistently quickly.

    Reply
  • 2019-05-11 at 09:16
    Permalink

    Thanks for sharing your thoughts about g. Regards

    Reply
  • 2019-05-11 at 09:27
    Permalink

    Pretty portion of content. I just stumbled upon your blog and in accession capital to say that I acquire actually loved account your weblog posts.
    Any way I’ll be subscribing for your feeds and even I
    success you get entry to persistently fast.

    Reply
  • 2019-05-12 at 00:03
    Permalink

    Hey there! I could have sworn I’ve been to this blog before
    but after checking through some of the post I realized it’s new to me.
    Anyways, I’m definitely delighted I found it and I’ll be bookmarking and checking back
    often!

    Reply
  • 2019-05-12 at 01:43
    Permalink

    Good post! We will be linking to this great post on our site.
    Keep up the good writing.

    Reply
  • 2019-05-12 at 15:21
    Permalink

    Every weekend i used to go to see this web site, because
    i want enjoyment, for the reason that this this web page conations genuinely good funny data too.

    Reply
  • 2019-05-13 at 09:48
    Permalink

    Heya i am for the first time here. I came across this board and I find It really useful
    & it helped me out a lot. I hope to give something back
    and aid others like you helped me.

    Reply
  • 2019-05-13 at 22:19
    Permalink

    My brother suggested I may like this web site. He was once totally right.
    This publish truly made my day. You cann’t imagine just how a lot
    time I had spent for this information! Thanks!

    Reply
  • 2019-05-15 at 06:53
    Permalink

    Good day very nice blog!! Guy .. Beautiful ..
    Amazing .. I’ll bookmark your web site and take the feeds also?
    I’m satisfied to seek out a lot of useful info right here within the put up, we want work out more strategies on this regard, thank you for sharing.
    . . . . .

    Reply
  • 2019-05-16 at 12:10
    Permalink

    I am really happy to read this blog posts which contains plenty of helpful data, thanks for providing these kinds of statistics.

    Reply
  • 2019-05-17 at 10:43
    Permalink

    Hi my family member! I wish to say that this post is awesome, nice written and come
    with approximately all vital infos. I would like to see extra posts like this .

    Reply
  • 2019-05-17 at 17:42
    Permalink

    Hi there to every body, it’s my first pay a quick visit of this web site;
    this weblog consists of amazing and genuinely
    good information in support of readers.

    Reply
  • 2019-05-18 at 10:40
    Permalink

    Hi, i think that i noticed you visited my blog so i came to go back the choose?.I’m attempting to find things to
    improve my web site!I guess its adequate to make use of a few of your
    ideas!!

    Reply

Leave a Reply to Kerry Cancel reply

Your email address will not be published. Required fields are marked *