php 传递多个连接表值以查看时 Laravel 5“尝试获取非对象的属性”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29480720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:25:55  来源:igfitidea点击:

Laravel 5 "Trying to get property of non-object" error when passing multiple join table values to view

phpmysqllaraveljoineloquent

提问by Hyman Barham

I am querying three tables using joins and getting the following error:

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

ErrorException: Trying to get property of non-object (View: .../views/app/text.blade.php).

ErrorException: Trying to get property of non-object (View: .../views/app/text.blade.php).

Let me explain the logic:

让我解释一下逻辑:

User: I need to grab details from the User model User: hasMany -> Page

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

Page: holds every page meta (title, slug, menu_order, active, etc). The page content is in separate model specific to that type of content. On this occasion Page: HasMany -> Text.

Page:保存每个页面元(标题、slug、menu_order、活动等)。页面内容位于特定于该类型内容的单独模型中。在此之际 Page: HasMany -> Text

Text: Is a type of page that has page content, it's related to the Page model Text: belongsTo -> Page.

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

Here is the 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'));
}

The joins seem to work because when I change return view('app.text', compact('text'));to return $textI get the following json response:

连接似乎有效,因为当我更改为时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": "Hyman@_ _ _.com",
        "username": "Hymanbarham",
        "full_name": "Hyman Barham",
        "country": "gb",
        "city": "London",
        "password": "_ _ _",
        "remember_token": null
    }
]

I'm not sure if this is related or not, the titlehas the value New orderin the database, but it doesn't appear in this output.

我不确定这是否相关,在数据库中title有值New order,但它没有出现在这个输出中。

My view:

我的看法:

{!! 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() !!}

I originally pulled the Pageand Usercontent via two separate calls (then two compacts) and it worked. However, now I have joined three tables I can't get the view to work without the errors.

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

I haven't put my model code on here as I'm sure they are working (re: json output) correctly - Although I'm happy to post more code if required.

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

回答by Hyman Barham

Changing ->get();to ->first();seems to have fixed it

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