Jack Barham
Jack Barham

Reputation: 3219

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

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).

Let me explain the logic:

User: I need to grab details from the User model 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.

Text: Is a type of page that has page content, it's related to the Page model 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 $text I get the following json response:

[
    {
        "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
    }
]

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

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 Page and User content 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.

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.

Upvotes: 1

Views: 23912

Answers (1)

Jack Barham
Jack Barham

Reputation: 3219

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

Upvotes: 3

Related Questions