MartinZeltin
1 month ago
138
2
Laravel

How to get only one keyBy value instead?

Posted 1 month ago by MartinZeltin

I have this settings table where I have key and value columns

+----+----------+-------------+
| id | key      | value       |
+----+----------+-------------+
| 1  | logo_img | my-logo.jpg |
| 2  | version  | 1.1         |
+----+----------+-------------+

and I would like to get the results like this.

"settings": {
  "logo_img": "my-logo.jpg",
  "version": 1.1,
}

But instead I get the results like this if I use keyBy()

$settings = Settings::all()->keyBy('key');

Results:

"settings": {
  "logo_img": {
    "id": 1,
    "key": "logo_img",
    "value": "my-logo.jpg"
  },

  "version": {
    "id": 2,
    "key": "version",
    "value": "1.1"
  }
}

In the end I would like to refer to settings like this $settings->logo_img instead of $settings->logo_img->value

Not sure how to do this

Please sign in or create an account to participate in this conversation.