skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. $companyContents = Homepage::where('section', 'Company Name')
                   ->first()
                   ->pluck('contents');
    
    echo $companyContents;
    

    You can use pluck instead of select, this will return only the relevant columns of a Model

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search