Search code examples
laraveleloquentlaravel-blade

How to use Laravel Eloquent to pass a single row of data to View?


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');

Solution

  • So if you are trying to get the first item. You can try

    $companyName = Homepage::select('contents')->where('section', 'Company Name')->first();
    echo $companyName->contents;
    

    As to why first() instead of get(), then get()returns a collection where as first() returns a single model instance.