Erii
288
11
Eloquent

Eloquent to object

Posted 8 months ago by Erii

Morning! So, I've stumbled upon a problem with Laravel Eloquent. I can't get it to work as I want it to.. The thing is, I have a db table which only have 2 columns (id not included) and I want to put ALL items directly to an object.

mysql> describe dnb_options;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| option_name  | varchar(255) | NO   |     | NULL    |                |
| option_value | longtext     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
mysql> select * from dnb_options;
+----+-------------+-------------------------------------------+
| id | option_name | option_value                              |
+----+-------------+-------------------------------------------+
|  1 | site_name   | my_site_name                              |
+----+-------------+-------------------------------------------+

I want to be able to do as following

$options = DNBOptions::get();

echo $options->site_name;

I've tried with some for-loops but I only mess things up. I want to be able to use this in controllers and views. This table will hold mostly static information,

Does anyone know how to do this? I can't remember how I did this before. If you need me to explain something, just ask, as it's a bit messy..

I don't know if I posted this in the wrong section, nor if I could get someone to understand this, but I hope someone can help me. I've done this before, but can't remember how I did it

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