| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\Model; |
| 7 |
use FluentCommunity\App\Models\XProfile; |
| 8 |
|
| 9 |
class SpaceUserPivot extends Model |
| 10 |
{ |
| 11 |
protected $table = 'fcom_space_user'; |
| 12 |
|
| 13 |
protected $guarded = ['id']; |
| 14 |
|
| 15 |
protected $fillable = [ |
| 16 |
'space_id', |
| 17 |
'user_id', |
| 18 |
'status', |
| 19 |
'role', |
| 20 |
'meta' |
| 21 |
]; |
| 22 |
|
| 23 |
public function scopeBySpace($query, $spaceId) |
| 24 |
{ |
| 25 |
if (!$spaceId) { |
| 26 |
return $query; |
| 27 |
} |
| 28 |
|
| 29 |
$query->where('space_id', $spaceId); |
| 30 |
|
| 31 |
return $query; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public function scopeByUser($query, $userId) |
| 36 |
{ |
| 37 |
if (!$userId) { |
| 38 |
return $query; |
| 39 |
} |
| 40 |
|
| 41 |
$query->where('user_id', $userId); |
| 42 |
|
| 43 |
return $query; |
| 44 |
} |
| 45 |
|
| 46 |
public function user() |
| 47 |
{ |
| 48 |
return $this->belongsTo(User::class, 'user_id', 'ID'); |
| 49 |
} |
| 50 |
|
| 51 |
public function xprofile() |
| 52 |
{ |
| 53 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 54 |
} |
| 55 |
|
| 56 |
public function space() |
| 57 |
{ |
| 58 |
return $this->belongsTo(BaseSpace::class, 'space_id', 'id')->withoutGlobalScopes(); |
| 59 |
} |
| 60 |
|
| 61 |
public function setMetaAttribute($value) |
| 62 |
{ |
| 63 |
$this->attributes['meta'] = maybe_serialize($value); |
| 64 |
} |
| 65 |
|
| 66 |
public function getMetaAttribute($value) |
| 67 |
{ |
| 68 |
if (!$value) { |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
if (is_string($value)) { |
| 73 |
$decodedMeta = json_decode($value, true); |
| 74 |
if (is_array($decodedMeta)) { |
| 75 |
$value = $decodedMeta; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$value = Utility::safeUnserialize($value); |
| 80 |
|
| 81 |
if (!is_array($value)) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
return $value; |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|