Show the code show.blade.php
Trying to get property of non-object - for authorized users only
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', 'HomeController@index')->name('home');
Route::get('{provider}/auth', [
'uses' => 'SocialsController@auth',
'as' => 'social.auth'
]);
Route::get('/{provider}/redirect', [
'uses' => 'SocialsController@auth_callback',
'as' => 'social.callback'
]);
Route::get('rooms/index', [
'uses' => 'RoomsController@index',
'as' => 'rooms.index'
]);
Route::get('rooms/{slug}', [
'uses' => 'RoomsController@show',
'as' => 'room'
]);
Route::group(['middleware'=>'auth'], function() {
Route::get('rooms/create', [
'uses' => 'RoomsController@create',
'as' => 'rooms.create'
]);
Route::post('rooms/store', [
'uses' => 'RoomsController@store',
'as' => 'rooms.store'
]);
Route::get('rooms/edit/{slug}', [
'uses' => 'RoomsController@edit',
'as' => 'rooms.edit'
]);
Route::put('rooms/update/{id}', [
'uses' => 'RoomsController@update',
'as' => 'rooms.update'
]);
Route::delete('rooms/delete/{slug}', [
'uses' => 'RoomsController@destroy',
'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
@Snapey - > Sorry for the confusion. I was such an idiot!
I figured out the problem.
Here is how I had my web.php before
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('{provider}/auth', [
'uses' => 'SocialsController@auth',
'as' => 'social.auth'
]);
Route::get('/{provider}/redirect', [
'uses' => 'SocialsController@auth_callback',
'as' => 'social.callback'
]);
Route::get('rooms/index', [
'uses' => 'RoomsController@index',
'as' => 'rooms.index'
]);
Route::get('rooms/{slug}', [
'uses' => 'RoomsController@show',
'as' => 'room'
]);
Route::group(['middleware'=>'auth'], function() {
Route::get('rooms/create', [
'uses' => 'RoomsController@create',
'as' => 'rooms.create'
]);
Route::post('rooms/store', [
'uses' => 'RoomsController@store',
'as' => 'rooms.store'
]);
Route::get('rooms/edit/{slug}', [
'uses' => 'RoomsController@edit',
'as' => 'rooms.edit'
]);
Route::put('rooms/update/{id}', [
'uses' => 'RoomsController@update',
'as' => 'rooms.update'
]);
Route::delete('rooms/delete/{slug}', [
'uses' => 'RoomsController@destroy',
'as' => 'rooms.destroy'
]);
});
Notice how the rooms/{slug} is above rooms/create. Well, the problem with this is that it thinks that "create" is a slug and it can't find it in the database.
I figured this out by going to the RoomsController and putting in dd($slug); into the show function. When it told me "create" I found out what was the problem.
To fix this, I needed to not only put the show route at the bottom of the web.php file, but also outside of the auth middleware.
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('{provider}/auth', [
'uses' => 'SocialsController@auth',
'as' => 'social.auth'
]);
Route::get('/{provider}/redirect', [
'uses' => 'SocialsController@auth_callback',
'as' => 'social.callback'
]);
Route::get('rooms/index', [
'uses' => 'RoomsController@index',
'as' => 'rooms.index'
]);
Route::group(['middleware'=>'auth'], function() {
Route::get('rooms/create', [
'uses' => 'RoomsController@create',
'as' => 'rooms.create'
]);
Route::post('rooms/store', [
'uses' => 'RoomsController@store',
'as' => 'rooms.store'
]);
Route::get('rooms/edit/{slug}', [
'uses' => 'RoomsController@edit',
'as' => 'rooms.edit'
]);
Route::put('rooms/update/{id}', [
'uses' => 'RoomsController@update',
'as' => 'rooms.update'
]);
Route::delete('rooms/delete/{slug}', [
'uses' => 'RoomsController@destroy',
'as' => 'rooms.destroy'
]);
});
Route::get('rooms/{slug}', [
'uses' => 'RoomsController@show',
'as' => 'room'
]);
Now, everything is working! :)
Thanks again everyone who has spent the time to help me! I really appreciate your feedback and ideas.
And, sorry this was such a stupid mistake. It seems obvious now.
Thanks again,
Brad
Please or to participate in this conversation.