Trying to get property of non-object - for authorized users only

Posted 1 year ago by almost_pitt

Hi guys,

I'm having a weird problem that I can't see to figure out.

I am using the typical CRUD method. When I put the show route under the auth middleware, I can see it by authorized users, but of course, not by unauthorized users. Weirdly enough, when I put the show route outside of the auth middleware, then authorized users cannot see the create page. I'm really confused.

Let me share some code: (Here, the unauthorized users can see the posts, but when an authorized user tries to create a new post, he gets the following error

(2/2) ErrorException Trying to get property of non-object (View: /Users/code/resources/views/rooms/show.blade.php)

web.php

<?php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', '[email protected]')->name('home');

Route::get('{provider}/auth', [
  'uses' => '[email protected]',
  'as' => 'social.auth'
  ]);
Route::get('/{provider}/redirect', [
  'uses' => '[email protected]_callback',
  'as' => 'social.callback'
  ]);
Route::get('rooms/index', [
  'uses' => '[email protected]',
  'as' => 'rooms.index'
  ]);
  Route::get('rooms/{slug}', [
  'uses' => '[email protected]',
  'as' => 'room'
  ]);

Route::group(['middleware'=>'auth'], function() {

  Route::get('rooms/create', [
    'uses' => '[email protected]',
    'as' => 'rooms.create'
    ]);
  Route::post('rooms/store', [
    'uses' => '[email protected]',
    'as' => 'rooms.store'
    ]);
  Route::get('rooms/edit/{slug}', [
    'uses' => '[email protected]',
    'as' => 'rooms.edit'
    ]);
  Route::put('rooms/update/{id}', [
    'uses' => '[email protected]',
    'as' => 'rooms.update'
    ]);
  Route::delete('rooms/delete/{slug}', [
    'uses' => '[email protected]',
    'as' => 'rooms.destroy'
    ]);
});

RoomsController.php


...
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('rooms.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate(request(), [
            'title' => 'required|max:45|min:10',
...
        ]);

        $room = Room::create([
            'title' => request()->title,
...
        ]);

        return redirect()->route('room', ['slug' => $room->slug]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($slug)
    {
        return view('rooms.show')->with('rooms', Room::where('slug', $slug)->first());
    }

Is there anyway to perhaps put the show route available to both authorized and non-authorized users? (This seems to be the case for everything outside of the auth middleware.... but, at this point, I'm not really sure.)

Thanks a lot for any ideas or help that you can provide. You guys have been really great!

Thanks!

Brad

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.