我有一个名为 Secret 的对象,我希望在其中对“secret_title”和“secret_text”这两个字段进行加密。当用户登录时,我想在表格中显示他们的秘密列表。我在 Secret 类中使用 setter 和 getter 来加密和解密信息,但是当我在视图中打印 secret_title 和 secret_text 时,它仍然是加密的。这是我的对象:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption;
class Secret extends Model
{
protected $fillable = [
'user_id',
'secret_title',
'secret_text',
];
public function getSecretTitleAttribute()
{
return Crypt::decrypt($this->attributes['secret_title']);
}
public function getSecretTextAttribute()
{
return Crypt::decrypt($this->attributes['secret_text']);
}
public function setSecretTitleAttribute($value)
{
$this->attributes['secret_title'] = Crypt::encrypt($value);
}
public function setSecretTextAttribute($value)
{
$this->attributes['secret_text'] = Crypt::encrypt($value);
}
}
在我使用的控制器中:
public function showUserSecrets()
{
$user_id = Auth::id();
$secrets = DB::table('secrets')->where('user_id', $user_id)->get();
return view('secret.show', compact('secrets'));
}
在我看来,我有:
@foreach ($secrets as $secret)
<tr>
<td>{{ $secret->id }}</td>
<td>{{ $secret->secret_title }}</td>
<td>{{ $secret->created_at }}</td>
<td><a href="{{ route('secret.show', $secret->id) }}" class="button">View</a></td>
</tr>
@endforeach
jeck猫