I am very new to Laravel. I am still learning the concepts. I tried to google this problem but i don’t think I know what to type?
I am retrieving a single row, single column of data from my database using this code in my controller.
$companyName = Homepage::select('contents')->where('section', 'Company Name') ->get();
echo $companyName;
The echo showed in my view is:
[{"contents":"Example Company Ltd"}]
I know this is pretty basic but what can I do to make sure it only echo "Example Company Ltd"?
Thank you for any help answering this. I am really new.
I tried something like
echo $companyName->get('contents');
2
Answers
So if you are trying to get the first item. You can try
As to why
first()
instead ofget()
, thenget()
returns a collection where asfirst()
returns a single model instance.You can use pluck instead of select, this will return only the relevant columns of a Model