Custom encryption trait breaks carbon functionality and gives error: The payload is invalid

Custom encryption trait breaks carbon functionality and gives error: The payload is invalid

我在我的 Journal 模型上使用自定义加密特征。我没有创建此代码并模糊地理解它是如何工作的,but the original code can be found here on this Laracast forum post.

Encryption.php

<?php
namespace App;

use Illuminate\Support\Facades\Crypt;
trait Encryptable
{
    public function getAttribute($key)
    {
        $value = parent::getAttribute($key);

        if (in_array($key, $this->encryptable)) {
            $value = Crypt::decrypt($value);
            return $value;
        }

        return $value;
    }

    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->encryptable)) {
            $value = Crypt::encrypt($value);
        }

        return parent::setAttribute($key, $value);
    }
}

Journal.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Journal extends Model
{
    use Encryptable;

    protected $encryptable = [
        'content'
    ];
    protected $fillable = ['content','user_id'];

}

我遇到了两个问题:

  1. Call to a member function toRfc822String() on null (View: /Applications/MAMP/htdocs/thought-records/resources/views/journal/index.blade.php)

  2. 当我从 blade 文件中删除 toRfc822String() 时,它会抛出此错误:负载无效。

这是index.blade.php

<div class="card-body">
    @if($entries->isEmpty())
        <p>There is nothing here!</p>
    @else
        @foreach($entries as $entry)
            <a href="/entry/{{$entry->id}}"><h3>{{ $entry->created_at }}</h3></a>
            <div v-html="markdown('{{ htmlentities($entry->content) }}')"> </div>
            <hr>
        @endforeach
    @endif
</div>

getAttribute 方法必须 return 一些东西。您的方法 return 无效。因此,您尝试通过动态 属性、$model->attribute 访问的每个属性都将是 null