0

I cant figure out why, but I can var_dump($cPage) and get the complete array, but I can't access my variable in the view. {{ $cPage->code }} allways gets me an "Undefined property: Illuminate\Database\Eloquent\Collection::$code" error. Can anyone explain me why?

Controller:

public function getPages($lang, $slug)
{

    $cPage = Page::join('langs', 'langs.id', '=', 'pages.lang_parent_id')
            ->where('slug', '=', $slug)
            ->where('code', '=', $lang)
            ->get();

    $mainPages = Page::where('parent_id', null)
                ->get();

    $allPages = $this->getAllPages($mainPages);

    return View::make('index')
    ->with('cPage', $cPage)
    ->with('mainPages', $mainPages)
    ->with('allPages', $allPages);

}   

View

<ul class="nav">
  <li class="dropdown">
    <button type="submit" class="btn dropdown-toggle icon-white" data-toggle="dropdown"> {{ $cPage->code }} <b class="caret"></b></button>
    <ul class="dropdown-menu pull-right">
      <li><a href="#">Português</a></li>
      <li><a href="#">English</a></li>
    </ul>
  </li>
</ul>

var_dump($cPage)

array(1) { [0]=> array(9) { ["id"]=> string(1) "1" ["parent_id"]=> NULL ["lang_parent_id"]=> string(1) "1" ["translate_parent_id"]=> NULL ["title"]=> string(8) "Clientes" ["slug"]=> string(8) "clientes" ["sort"]=> NULL ["name"]=> string(10) "Português" ["code"]=> string(2) "pt" } }

3
  • Try ->first() instead of get Commented Nov 4, 2014 at 18:04
  • Thanks, it worked with ->first(). Can you offer me an explanation for this? Commented Nov 4, 2014 at 18:07
  • Your query potentially returns multiple results so get returns a collection (even if its just one result). First limits the query to one result and returns that Commented Nov 4, 2014 at 18:11

1 Answer 1

1

You need to use loop in your view, or if you expect only one result, you can change get into first:

$cPage = Page::join('langs', 'langs.id', '=', 'pages.lang_parent_id')
        ->where('slug', '=', $slug)
        ->where('code', '=', $lang)
        ->first();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.