Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jackbarham's avatar

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.

0 likes
8 replies
bestmomo's avatar

Form::model if for model binding, you dont pass a model in $text.

jackbarham's avatar

I've actually just figured out what it was.

I needed to change ->get(); to ->first();

2 likes
dhaval.v's avatar

Hello guys, you've nothing change in your view but put your route into middleware web.

Route::group(['middleware' => ['web']],function(){
    Route::get('support',function(){
            return view('support');
    });
});
bobbybouwmann's avatar

@dhaval.v Woooww... This thread is a year old.. Middleware groups didn't exist back then... Also this is not a middleware group issue, it had indeed something to do with fetching the correct data.

idhamhafidz's avatar

You have to trace back. This error occurs because relationship of tables does not return any data and you try to printout it in view.

Try using php artisan tinker, check your data by following the same flow you have on your controller that leads to your view.

2 likes
rjony's avatar

Hi, Just simply change ->get(); to ->first();

Hope it works!

2 likes
matrint's avatar

maybe it make problem by the view cache!

Please or to participate in this conversation.