使用Laravel 5从数据库检索字段时出错

时间:2015-10-13 15:44:35

标签: php laravel eloquent

具有控制器功能:

public function pageedit($id)
{
    $page = Pages::where('pgeID','=',121)->get();
    return view('admin.pageedit')->with('page',$page);
}

一个简单的观点:

@extends('admin')

@section('content')
    @include('/partials/adminpageheader')
    Edit Page {{ $page }}
@endsection

当我运行它时,我得到如下输出:

Intranet Web Control Panel (Pages)
Admin Home - Admin Home Page
Edit Page
[{"pgeID":"121","pgeActive":"0","pgeContent":"1","pgeMainLevel":"6","pgeLevel":"1","pgeParent":"6","pgeOrder":"3","pgeTitle":"Employee Assistance Program","pgeHeader":"Null","pgeBody":Employee Assistance Program that all employees can access 24\/7. The website, www.assist.com has an amazing amount of information available. If you are looking for training, articles, resources and more, check them out. The use","pgeNav":null,"pgeContents":"Null","pgeCallOut":"Null","pgeEditor":"Null","pgeTitleDRAFT":"Null","pgeTemplateDRAFT":null,"pgeNavDRAFT":null,"pgeContentsDRAFT":"Null","pgeCallOutDRAFT":"Null"}]

所以我知道它正在正确地提取数据。但是当我尝试显示JUST页面标题(pgeTitle)时,如果我尝试{{$ page-> pgeTitle}}我会得到一个未定义的属性错误,如果我尝试{{$ page [' pgeTitle& #39;]}}

我知道它必须是我忘记的简单事物,但如果我能弄明白它是什么,我将会感到危险。想法?

3 个答案:

答案 0 :(得分:0)

$page是一个数组,据我所知,索引是数字的,所以你应该这样做:

{{$page[0]->pgeTitle}}

答案 1 :(得分:0)

而不是使用->get()使用->first()。这将返回单个实例而不是集合,这是必须使用数组访问器之间的区别。

$page = Pages::where('pgeID','=',121)->first();
                                         ^
                                    first==single
                                get==array collection

答案 2 :(得分:0)

与Taxicala相同的答案,但我建议您使用 - > first()方法而不是get,因为您只需要查找1行。

$page = Pages::where('pgeID','=',121)->first();