错误使用Laravel 5
问题描述:
从数据库中检索领域有一个控制器功能:错误使用Laravel 5
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']}}
我知道它必须是简单的东西,我忘了,但如果我能弄清楚它是什么,我会被打断。思考?
答
而不是使用->get()
使用->first()
的。这将返回一个单独的实例,而不是集合,这是必须使用数组访问器与不使用数组之间的区别。
$page = Pages::where('pgeID','=',121)->first();
^
first==single
get==array collection
答
$page
是一个数组,其中指数是数字,据我所知道的,所以你应该做的:
{{$page[0]->pgeTitle}}
答
相同的答案比Taxicala,但我建议你使用 - >第()方法,而不是得到,因为你要找的只是1行。
$page = Pages::where('pgeID','=',121)->first();
选择以下答案是正确的,只是因为用第一,我不再满足它作为$页[0] - > pgeTitle,jusr $页面级> pgeTitle –
@KeithClark不是问题! –