@gibex This is the encryptable trait again
namespace App\Traits;
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;
}
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable)) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
}
This is the model using the trait
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Traits\Encryptable;
class MemberDetails extends Model
{
use Encryptable;
protected $table = 'bankdetails';
protected $fillable = [
'name',
'branch',
'address',
'city',
'postCode',
'completed'
];
protected $encryptable = [
'branch',
'postCode',
'city'
];
}
I have not added a custom update method, I use the one extended from Model
eg.
$user->memberDetails()->update($request->except('_token'));