api person create & update & add family
This commit is contained in:
22
app/Models/Family.php
Normal file
22
app/Models/Family.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Family extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'family_relations';
|
||||
|
||||
protected $fillable = [
|
||||
'owner_id',
|
||||
'relation_with_owner',
|
||||
'person_id',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
}
|
||||
@@ -10,7 +10,7 @@ use Illuminate\Support\Str;
|
||||
class File extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
|
||||
protected $fillable = [
|
||||
'fileable_type',
|
||||
'fileable_id',
|
||||
@@ -20,12 +20,10 @@ class File extends Model
|
||||
'path',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'url'
|
||||
];
|
||||
|
||||
public static $file_directories = [
|
||||
'import-temp' => 'import-temp/',
|
||||
'avatar' => 'user-avatar/',
|
||||
'dataDiri' => 'data-diri/'
|
||||
];
|
||||
|
||||
public function fileable()
|
||||
@@ -33,29 +31,23 @@ class File extends Model
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
return Storage::url($this->path);
|
||||
}
|
||||
|
||||
public function getDirectory($type)
|
||||
public static function getDirectory($type)
|
||||
{
|
||||
return self::$file_directories[$type] ?? 'any';
|
||||
}
|
||||
|
||||
public static function getFileName($type, $id, $file)
|
||||
public static function getFileName($type, $id)
|
||||
{
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
|
||||
return $type . '-' . $id .'-'.Str::random(10).'.'.$extension;
|
||||
return $type . '-' . $id . '-' . Str::random(10);
|
||||
}
|
||||
|
||||
public static function storeFile($type, $id, $file)
|
||||
{
|
||||
$fileName = self::getFileName($type, $id, $file);
|
||||
$fileName = self::getFileName($type, $id);
|
||||
$directory = self::getDirectory($type);
|
||||
$path = $directory . $fileName;
|
||||
$file->storeAs($directory, $fileName);
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
$path = $directory . $fileName . '.' . $extension;
|
||||
$file->storeAs('public/' . $directory, $fileName . '.' . $extension);
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,20 +10,33 @@ class Person extends Model
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'persons';
|
||||
|
||||
|
||||
protected $fillable = [
|
||||
'owner_user_id',
|
||||
'nik',
|
||||
'name_prefix',
|
||||
'name',
|
||||
'name_suffix',
|
||||
'phone',
|
||||
'email',
|
||||
'gender',
|
||||
'birth_date',
|
||||
'birth_place',
|
||||
'citizenship',
|
||||
'current_employment',
|
||||
'last_education',
|
||||
'religion',
|
||||
'blood_type',
|
||||
'weight',
|
||||
'height',
|
||||
'is_deceased',
|
||||
'deceased_at',
|
||||
'marital_status',
|
||||
'main_address_id',
|
||||
'domicile_address_id',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by'
|
||||
];
|
||||
|
||||
public function getFullNameAttribute()
|
||||
@@ -31,11 +44,11 @@ class Person extends Model
|
||||
$arr = [];
|
||||
if (!empty($this->name_prefix)) {
|
||||
$arr[] = $this->name_prefix;
|
||||
}
|
||||
}
|
||||
$arr[] = $this->name;
|
||||
if (!empty($this->name_suffix)) {
|
||||
$arr[] = $this->name_suffix;
|
||||
}
|
||||
}
|
||||
|
||||
return implode(' ', $arr);
|
||||
}
|
||||
@@ -44,17 +57,17 @@ class Person extends Model
|
||||
{
|
||||
return $this->morphMany(Address::class, 'addressable');
|
||||
}
|
||||
|
||||
|
||||
public function currentAddress()
|
||||
{
|
||||
return $this->belongsTo(Address::class, 'main_address_id');
|
||||
}
|
||||
|
||||
|
||||
public function domicileAddress()
|
||||
{
|
||||
return $this->belongsTo(Address::class, 'main_address_id');
|
||||
}
|
||||
|
||||
|
||||
public function metas()
|
||||
{
|
||||
return $this->morphMany(Meta::class, 'metaable');
|
||||
@@ -65,26 +78,18 @@ class Person extends Model
|
||||
return $this->belongsTo(User::class, 'owner_user_id');
|
||||
}
|
||||
|
||||
public function makeLmsApiData()
|
||||
public function files()
|
||||
{
|
||||
return [
|
||||
'name' => $this->name,
|
||||
'birth_date' => $this->birth_date,
|
||||
'birth_place' => $this->birth_place,
|
||||
'gender' => $this->gender,
|
||||
'blood_type' => $this->blood_type,
|
||||
'religion' => $this->religion,
|
||||
'last_education' => $this->last_education,
|
||||
'current_employment' => $this->current_employment,
|
||||
'marital_status' => $this->marital_status,
|
||||
'nik' => $this->nik,
|
||||
'citizenship' => $this->citizenship,
|
||||
'address' => $this->currentAddress->text ?? null,
|
||||
'city_name' => $this->currentAddress->city->name ?? null,
|
||||
'domicile_address' => $this->domicileAddress->text ?? null,
|
||||
'domicile_city_name' => $this->domicileAddress->city->name ?? null,
|
||||
'email' => $this->email,
|
||||
'phone' => $this->phone,
|
||||
];
|
||||
return $this->morphMany(File::class, 'fileable');
|
||||
}
|
||||
|
||||
public function avatar()
|
||||
{
|
||||
return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latestOfMany();
|
||||
}
|
||||
|
||||
public function familyOwner()
|
||||
{
|
||||
return $this->hasOne(Family::class, 'person_id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user