You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Institution.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Models\Banking;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. use Illuminate\Support\Facades\Storage;
  8. class Institution extends Model
  9. {
  10. use HasFactory;
  11. protected $table = 'institutions';
  12. protected $fillable = [
  13. 'external_institution_id',
  14. 'name',
  15. 'logo',
  16. 'website',
  17. 'phone',
  18. 'address',
  19. 'created_by',
  20. 'updated_by',
  21. ];
  22. protected $appends = [
  23. 'logo_url',
  24. ];
  25. public function bankAccounts(): HasMany
  26. {
  27. return $this->hasMany(BankAccount::class, 'institution_id');
  28. }
  29. public function connectedBankAccounts(): HasMany
  30. {
  31. return $this->hasMany(ConnectedBankAccount::class, 'institution_id');
  32. }
  33. protected function logoUrl(): Attribute
  34. {
  35. return Attribute::get(static function (mixed $value, array $attributes): ?string {
  36. if ($attributes['logo']) {
  37. return Storage::disk('public')->url($attributes['logo']);
  38. }
  39. return null;
  40. });
  41. }
  42. public function logo(): Attribute
  43. {
  44. return Attribute::set(static function (mixed $value): ?string {
  45. if ($value) {
  46. $decoded = base64_decode($value);
  47. $filename = 'institution_logo_' . uniqid('', true) . '.png';
  48. Storage::disk('public')->put($filename, $decoded);
  49. return $filename;
  50. }
  51. return null;
  52. });
  53. }
  54. }