Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F8199741
UserSettingsTrait.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
UserSettingsTrait.php
View Options
<?php
namespace
App\Traits
;
use
App\UserSetting
;
use
Illuminate\Support\Facades\Cache
;
trait
UserSettingsTrait
{
// get setting value
public
function
getSetting
(
$name
)
{
$settings
=
$this
->
getCache
();
$value
=
array_get
(
$settings
,
$name
);
return
(
$value
!==
''
)
?
$value
:
NULL
;
}
// create-update setting
public
function
setSetting
(
$name
,
$value
)
{
$this
->
storeSetting
(
$name
,
$value
);
$this
->
setCache
();
}
// create-update multiple settings at once
public
function
setSettings
(
$data
=
[])
{
foreach
(
$data
as
$name
=>
$value
)
{
$this
->
storeSetting
(
$name
,
$value
);
}
$this
->
setCache
();
}
private
function
storeSetting
(
$name
,
$value
)
{
$record
=
UserSetting
::
where
([
'user_id'
=>
$this
->
id
,
'key'
=>
$name
])->
first
();
if
(
$record
)
{
$record
->
value
=
$value
;
$record
->
save
();
}
else
{
$data
=
new
UserSetting
([
'key'
=>
$name
,
'value'
=>
$value
]);
$this
->
settings
()->
save
(
$data
);
}
}
private
function
getCache
()
{
if
(
Cache
::
has
(
'user_settings_'
.
$this
->
id
))
{
return
Cache
::
get
(
'user_settings_'
.
$this
->
id
);
}
return
$this
->
setCache
();
}
private
function
setCache
()
{
if
(
Cache
::
has
(
'user_settings_'
.
$this
->
id
))
{
Cache
::
forget
(
'user_settings_'
.
$this
->
id
);
}
$settings
=
$this
->
settings
()->
get
();
Cache
::
forever
(
'user_settings_'
.
$this
->
id
,
$settings
);
return
$this
->
getCache
();
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Thu, Jul 9, 12:11 AM (21 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1052316
Default Alt Text
UserSettingsTrait.php (1 KB)
Attached To
Mode
R2 kolab
Attached
Detach File
Event Timeline
Log In to Comment