0

I have a database table with the following structure.

id setting_name setting_value
1 website_name New Website
2 contact_email [email protected]
3 contact_address London, UK

Now I am using laravel to return the values of this table,

$syetem_settings = System::all()

This returns it as a collection and there is no way I can use the individual values

$syetem_settings = System::all()->toArray()

This also returns it as an array and foreach loop isn't helping me.

What I want to do I want to use this variables in the view blade. e.g

{{system_settings->website_name}} //  to return website name;
{{system_settings->contact_email}} //  to return website email;

Please what is the right code for this? I'm using php 8 and laravel 9

3
  • this is a bad database design in my opinion. The rows (settings_name) should have been columns in the first place. Commented May 21, 2022 at 2:24
  • I understand your point, i'm seeking to know if this achievable Commented May 21, 2022 at 2:34
  • Arr::collapse should do the trick but will require more customization. Commented May 21, 2022 at 2:55

2 Answers 2

2

If you really must have this database design, then you would need to get each setting by name from the database as single models, like this:

$websiteName = System::where('setting_name', 'website_name')->first();
$contactEmail = System::where('setting_name', 'contact_email')->first();

Then just output it like this:

{{ $websiteName }}
{{ $contactEmail }}

But if you want to have all the settings variables in one single object, then you would need all the setting_name values as actual columns. So you get ONE single model with all the values instead of one model per value.

This might not be what you want, for other reasons. So a workaround for you, with your current database design, could be something like this:

// Get all rows
$systemSettings = System::all();

// Create empty object
$settingsObject = new stdClass;

// Loop through all the rows (models)
foreach($systemSettings as $systemSetting) {
    // Get the settings_name to use as attribute name
    $attribute = $systemSetting->setting_name;
    
    // Get the settings_value to use as value for the attribute
    $value = $systemSetting->setting_value;

    // Add attribute and value to the empty object
    $settingsObject->$attribute = $value;
}

This should give you an object called $settingsObject that has all the rows as attribute->value. And then you could output your stuff like this:

{{ $settingsObject->website_name }} 
{{ $settingsObject->contact_email }} 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you're trying to use Entity-Value architecture for your table. It could be a good idea to pluck each column data separately and then combine them as a single array:

$keys = System::pluck('setting_name')->toArray();
$values = System::pluck('setting_value')->toArray();
$arr = array_combine($keys, $values);

then you can use foreach to itrate through the combined array:

foreach($arr as $k => $v) {
  ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.