我正在使用联接查询三个表,并得到以下错误:
CCD_ 1。
让我来解释一下逻辑:
User
:我需要从用户模型User: hasMany -> Page
中获取详细信息
Page
:保存每个页面元(标题、段塞、菜单顺序、活动等)。页面内容处于特定于该类型内容的单独模型中。在这个场合Page: HasMany -> Text
。
Text
:是一种具有页面内容的页面类型,它与页面模型Text: belongsTo -> Page
有关。
这是控制器:
public function show($id)
{
$text = DB::table('pages')->where('pages.id', '=', $id)
->join('texts', 'pages.id', '=', 'texts.page_id')
->join('users', 'pages.user_id', '=', 'users.id')->where('users.id', '=', Auth::user()->id)
->get();
return view('app.text', compact('text'));
}
连接似乎有效,因为当我将return view('app.text', compact('text'));
更改为return $text
时,我会得到以下json响应:
[
{
"id": 1,
"user_id": 1,
"title": "",
"slug": "new-order",
"type": "text",
"status": "private",
"order": 0,
"published_at": "2015-04-06 19:31:21",
"created_at": "2015-04-02 23:14:37",
"updated_at": "2015-04-03 15:57:08",
"page_id": 16,
"content": "testing",
"image_path": null,
"image_position": null,
"email": "jack@_ _ _.com",
"username": "jackbarham",
"full_name": "Jack Barham",
"country": "gb",
"city": "London",
"password": "_ _ _",
"remember_token": null
}
]
我不确定这是否相关,ErrorException: Trying to get property of non-object (View: .../views/app/text.blade.php)
0在数据库中的值为New order
,但它不会出现在这个输出中。
我的观点:
{!! Form::model($text, ['route' => ['text-update', $text->page_id], 'role'=> 'form', 'class' => 'page']) !!}
<div class="page__content">
<div class="ui form">
<div class="page__header">
<div class="field @if($errors->has('title')){{ 'error' }}@endif">
{!! Form::label('title', 'Page title') !!}
@if($errors->has('title'))<span class="validation__error">{{ $errors->first('title') }}</span>@endif
{!! Form::text('title', null, ['class' => 'js-slug__title']) !!}
<span class="page__slug">vybecast.com/{!! $text->username !!}/{!! Form::text('slug', null, ['class' => 'js-slug']) !!}</span>
</div>
</div><!-- page__header -->
<div class="field">
{!! Form::label('content', 'Page content') !!}
{!! Form::textarea('content', null) !!}
</div>
<div class="field">
<label>Page image</label>
{!! Form::file('hero', array('class' => 'form__file')) !!}
</div>
</div>
</div>
@include('app.layout.page-control')
{!! Form::close() !!}
最初,我通过两个单独的调用(然后是两个compact)提取了Page
和User
的内容,并且成功了。然而,现在我已经连接了三个表,如果没有错误,我就无法使视图正常工作。
我还没有把我的模型代码放在这里,因为我确信它们工作正常(re:json输出)——尽管如果需要,我很乐意发布更多代码。
将->get();
更改为->first();
似乎已经修复了