G
G
GrimJack2017-04-02 19:38:07
Laravel
GrimJack, 2017-04-02 19:38:07

How to get data in the form of an object in ELOQUENT ORM?

I can't figure out how to get data from a table as an object.
There is a table with two columns option (index field) and name. You need to get name from it where option = something.
Tried like this:

return OptionsModel::all->$name;
//либо вот так
return OptionsModel::where('option', $name)->get();

But nothing comes out. Google didn't give an answer.
UPD
This option:
return OptionsModel::where('option', "=", $name)->get();

Outputs this:
0 = {"option":"siteTitle","value":"WEBVOYAGER.PRO"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2017-04-03
@alexey-m-ukolov

The get() method returns a collection of objects, which you are trying to work with as a single object (this is what they write to you in the error).
If you obviously have one object, you need to do this:

return OptionsModel::where('option', $name)->first();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question