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

Neeraj1005's avatar

Trying to get property 'view_count' of non-object

i did not understand where I make a mistake. can anyone solve this problem?

This is my route

Route::get('{menu}/', 'contentviewController@show');

This is my controller file where I calling the route

    public function show($slug)
    {
        //

        //$content = Content::find($id);
       // $content = Content::where('slug', $slug)->first();
       $content = Content::where('slug', $slug)->first();
        
        $count = $content->view_count;
        $count = $count+1;
        $content->view_count = $count;
        $content->save();
        /*
        return view('viewcontent.index',compact('content'));
        */
        return view('theme1.viewcontent.index',compact('content'));
    }

But problem is that when I go to the admin panel and access this route http://127.0.0.1:8000/menu this will give an errors.

    Route::resource('menu','MenuController',
        ['names'=>[
            'index'=>'admin_menu',
            'create'=>'admin_menucreate',
            'edit'=>'admin_menu_edit'
        ]
    ]); 
0 likes
14 replies
Snapey's avatar

I'm confused, which is it you are talking about, the resource route or theRoute::get('{menu}/', route

By the way, this one will catch all routes so must be LAST in your routes file

Sinnbeck's avatar

What do you get if you try to dd right after getting the record? My guess is null

$content = Content::where('slug', $slug)->first();

dd($content, $slug);
Snapey's avatar

The actual error and error line would help

Snapey's avatar

As said, the ORDER of the lines in the web.php file is very important

From your question, its impossible to say if you are hitting the show method or the index method because they will both reply to GET of /menu

1 like
Neeraj1005's avatar

@snapey @sinnbeck In web.php route order would be consider because this is my route serialization

Route::get('{menu}/', 'contentviewController@show');

    Route::resource('menu','MenuController',
        ['names'=>[
            'index'=>'admin_menu',
            'create'=>'admin_menucreate',
            'edit'=>'admin_menu_edit'
        ]
    ]); 

But when I change the order no error hitting



    Route::resource('menu','MenuController',
        ['names'=>[
            'index'=>'admin_menu',
            'create'=>'admin_menucreate',
            'edit'=>'admin_menu_edit'
        ]
    ]); 

Route::get('{menu}/', 'contentviewController@show');
Snapey's avatar

But you will never reach than last line because accessing /menu will be captured by the index method

Did you actually mean

Route::get('menu', 'contentviewController@show');

or

Route::get('menu/{menu}', 'contentviewController@show');
Neeraj1005's avatar

@snapey basically I have used this Route::get('{menu}/', 'contentviewController@show'); for a getting the slug url.

Sinnbeck's avatar

If you expect it to work with both, then there is no way that will ever work. Perhaps you should consider adding more to the wildcard url to avoid conflicts

Route::get('content/{menu}/', 'contentviewController@show');
1 like

Please or to participate in this conversation.