php - Laravel 5 "Trying to get property of non-object"传递多个连接表值查看时出错

标签 php mysql laravel join eloquent

我正在使用连接查询三个表并收到以下错误:

ErrorException:尝试获取非对象的属性( View :.../views/app/text.blade.php)

让我解释一下逻辑:

User:我需要从 User 模型中获取详细信息 User: hasMany -> Page

Page:包含每个页面元数据(标题、slug、menu_order、事件等)。页面内容位于特定于该类型内容的单独模型中。在这种情况下 Page: HasMany -> Text

Text:是一种有页面内容的页面,它与Page模型Text: belongsTo -> Page有关。

这是 Controller :

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
    }
]

我不确定这是否相关,title 在数据库中有值 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() !!}

我最初通过两个单独的调用(然后是两个压缩包)提取了 PageUser 内容并且它起作用了。但是,现在我已经连接了三个表,我无法在没有错误的情况下使 View 正常工作。

我没有将我的模型代码放在这里,因为我确定它们可以正常工作(回复:json 输出)- 尽管我很乐意在需要时发布更多代码。

最佳答案

->get(); 更改为 ->first(); 似乎已经修复了它

关于php - Laravel 5 "Trying to get property of non-object"传递多个连接表值查看时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29480720/

相关文章:

php - 使用 HTML 表单更新 MySQL

javascript - 如何将 onClick 事件添加到 PHP 生成的链接?

php - Newrelic 无法识别我的 Slim PHP 路由

php - Laravel Eloquent - 获取相关模型的最后插入

javascript - 在 Laravel 中基于选项标签渲染表格

php - 将用户数据存储在 csv 文件与数据库中

php - 仅发布使用 while 循环创建的表单中的选择值

php - 当我登录时安装 phabricator 后出现空白页面

mysql - 点燃表 |对 Group Concat 进行过滤会出现错误

php - Laravel 中 $errors 的含义是什么