1

I have a table on my DB with these columnns:

+-----------------+---------------------+------+-----+---------+----------------+
| Field           | Type                | Null | Key | Default | Extra          |
+-----------------+---------------------+------+-----+---------+----------------+
| id              | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name            | varchar(255)        | NO   |     | NULL    |                |
| value           | varchar(255)        | NO   |     | NULL    |                |
+-----------------+---------------------+------+-----+---------+----------------+

I would access to value values by name.

For example I have these datas:

+----+------------+------------+
| id | name       | value      |
+----+------------+------------+
|  1 | start_date | 2020-01-01 |
|  2 | end_date   | 2021-01-01 |
+----+------------+------------+

I would to get '2020-01-01' by 'start_date'.

I tried this code, but I'm not satisfyed because with this code I get all values of the row, not only the value expected.

Configuration::get()->keyBy('start_date');

I'm not sure I was clear.

Let me know.

Thanks a lot!!

7
  • What have you tried? I'm not seeing any Models or Queries or anything in your question... On Stackoverflow, you're expected to make an effort to solve you own problem, and we'll assist with debugging your approach. Commented Feb 26, 2021 at 20:07
  • 1
    @TimLewis I added my code. Thanks for your suggestion! Commented Feb 26, 2021 at 20:12
  • No problem! Next step, just a quick explanation on why that code doesn't work :) What does it do vs what you're expecting it to do. At a glance, maybe Configuration::pluck('name', 'value') might work (or ::pluck('value', 'name'), not sure on the order) Commented Feb 26, 2021 at 20:12
  • 1
    Using the code I posted, you should end up with a Collection like this: ['start_date' => '2020-01-01', 'end_date' => '2021-01-01'], so if you did $keyVals = Configuration::pluck('value', 'name');, then you'd be able to access it like $keyVals['start_date'] ('2020-01-01') and $keyVals['end_date'] ('2021-01-01'). Is that what you're looking for? If not, then I really don't understand your question... Commented Feb 26, 2021 at 21:01
  • 1
    I think it could be good for me! I will try in my code :) Thanks in advance Commented Feb 26, 2021 at 21:04

1 Answer 1

1

Assuming you want to get an array of key/value pairs, and each key in the name column is unique, you can simply use pluck() (https://laravel.com/docs/8.x/collections#method-pluck):

$configuration = Configuration::pluck('value', 'name');
dd($configuration);
// ['start_date' => '2020-01-01', 'end_date' => '2021-01-01']

Then, you'd use simply array access to use these configuration settings where applicable:

$startDate = $configuration['start_date']; // '2020-01-01'
$endDate = $configuration['end_date'];     // '2021-01-01'
...
Sign up to request clarification or add additional context in comments.

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.