Laravel 自定义加密特性破坏了carbon功能并给出错误:有效负载无效
我在日志模型上使用自定义加密特性。我没有创建这个代码,只是模糊地理解它是如何工作的 Encryption.phpLaravel 自定义加密特性破坏了carbon功能并给出错误:有效负载无效,laravel,encryption,laravel-blade,laravel-6,Laravel,Encryption,Laravel Blade,Laravel 6,我在日志模型上使用自定义加密特性。我没有创建这个代码,只是模糊地理解它是如何工作的 Encryption.php <?php namespace App; use Illuminate\Support\Facades\Crypt; trait Encryptable { public function getAttribute($key) { $value = parent::getAttribute($key); if (in_arra
<?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);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Journal extends Model
{
use Encryptable;
protected $encryptable = [
'content'
];
protected $fillable = ['content','user_id'];
}
getAttribute
方法必须返回一些内容。您的方法返回void。因此,您尝试通过动态属性$model->attribute
访问的每个属性都将null
。我编辑了代码,以在getAttribute
方法中返回$value
。我尝试在tinker中检索记录,但它完全没有加密$entry=App\Journal::where('id',14)->first()代码>$entry->content
,但现在我必须在blade中返回一个空值,因为我得到了相同的负载异常。不过,碳排放的问题似乎已经解决。您可能遇到了一个$entry
,当您迭代时,内容没有加密值。。。在tinker中,您正在点击一条记录,您知道Worksah刚刚发现一条未加密的条目,这就是问题所在。谢谢
<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>