具有控制器功能:
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"}]
所以我知道它正在正确地提取数据。但是当我尝试显示页面标题(pgeTitle)我得到一个未定义的属性错误,如果我尝试{{$page->pgeTitle}}和一个未定义的索引错误,如果我尝试{{$page['pgeTitle']}}
我知道这一定是我忘记的一些简单的东西,但是如果我能弄清楚是什么,我会很危险的。想法吗?
用->first()
代替->get()
。这将返回单个实例,而不是集合,这是必须使用数组访问器和不使用数组访问器的区别。
$page = Pages::where('pgeID','=',121)->first();
^
first==single
get==array collection
$page
是一个数组,其中索引是数字,据我所知,所以你应该这样做:
{{$page[0]->pgeTitle}}
与Taxicala相同的答案,但我建议您使用->first()方法而不是get,因为您只查找1行。
$page = Pages::where('pgeID','=',121)->first();