D
D
dk-web2019-05-17 15:33:21
Laravel
dk-web, 2019-05-17 15:33:21

How to get (and should) a Laravel collection as an object?

The question is simple. But I forgot (I decided to rewrite the old project.
The table stores
uid, title, rus, value as follows.
I made a simple self-written admin panel for a one-pager about 4 years ago.
Accordingly, there are values ​​​​a la
1 'header' 'Header' 'Company ... '
2 'teaser' 'Text under the heading' 'Our company'
, etc.
I thought about quickly rewriting it in Laravel and immediately got stuck (
I haven't accessed data by key in an array for a long time.
And there everything was collected in $data. $data['mainpage']['header'] $data['services'] from another table, etc.
Accordingly, I do $mainpage = MainPage::pluck('value','title');
I get an array. And output only $mainpage['header']. Enrages). Get used to $mainpage->header etc.
Actually 2 questions.
1) How to translate the collection so that I can access it by property. Through foreach bulkhead?
2) How to optimize such a "trouble"?) Namely, with regard to the content of one-pagers. I use a package to store settings for my projects, which is basically key-value based.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Nikolaev, 2019-05-17
@Heian

Well, make it so that it doesn’t spit with a title, but some PageEntity, which will be a model or just an entity class. And there you will already use $mainpage->header.

V
Vitaly Yushkevich, 2019-05-19
@yushkevichv

You initially have a key-> value array. The collection will not help you for your desire.
If you want to change the access method, you can convert to stdobject either via (object) or via json double encoding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question