未签名时,ErrorException - 尝试获取非对象的 属性(视图:C:\xampp\...\show-solo.blade.php)
When NOT signed it, ErrorException - Trying to get property of non-object (View: C:\xampp\...\show-solo.blade.php)
大家好,只是想问一下Laravel像我这样学习的学生
登录后,我可以访问此页面:
Route::get('/posts/{id}', 'PostController@showById');
当我注销并尝试再次访问该页面时,我收到了提到的错误,我希望甚至为访客显示 posts,但不允许编写 post 如果他们已注销。
这里是 showById():
public function showById($id)
{
$post = Post::find($id);
return view('show-solo', compact('post'));
}
还有节目-solo.blade.php
@extends('master')
@include('partials.nav-none')
@section('content')
<div class="col-sm-8 blog-main">
<div class="blog-post">
@if ($flash = session('message'))
<div class="alert alert-success flash-message" role="alert">
{{ $flash }}
</div>
@endif
@if ( $post->user_id == Auth::user()->id)
<a href="/posts/{{ $post->id }}/delete">
<button class="btn-sm btn-warning post-btn" data-toggle="tooltip" data-placement="top" title="Delete Post"><i class="fa fa-times"></i></button>
</a>
<a href="/posts/{{ $post->id }}/edit">
<button class="btn-sm btn-primary post-btn" data-toggle="tooltip" data-placement="top" title="Edit Post"><i class="fa fa-pencil-square-o"></i></button>
</a>
@endif
<h2>Post number: {{ $post->id }}</h2>
<h2 class="blog-post-title">
<a class="title-link" href="/posts/{{ $post->id }}">{{ $post->title }}</a>
</h2>
<!-- {{ $post->created_at->toFormattedDateString() }} -->
<p class="blog-post-meta">{{ $post->created_at->diffForHumans() }} by <a href="#">{{ $post->user->name }}</a></p>
{{ $post->body }}
<hr />
@include('partials.error')
@include('partials.post-comment')
</div>
</div>
@endsection
如果这有帮助,这是我的路线:
Route::get('/', function () {
return view('welcome');
});
Route::get('/posts', 'PostController@index')->name('home');
Route::get('/posts/create', 'PostController@showForm');
Route::get('/posts/{id}', 'PostController@showById');
Route::get('posts/{id}/edit', 'PostController@editPostForm');
Route::get('posts/{id}/delete', 'PostController@deletePost');
Route::post('/posts', 'PostController@store');
Route::post('/posts/{post}/comments', 'CommentController@store');
Route::get('posts/{id}/delete', 'CommentController@deleteComment');
Route::post('/save-post', 'PostController@savePost');
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/register-user', 'RegistrationController@create');
Route::post('/register-user', 'RegistrationController@store');
Route::get('/login-user', ['as' => '/login-user', 'uses' => 'SessionController@create']);
Route::post('/login-user', 'SessionController@store');
Route::get('/logout-user', 'SessionController@destroy');
我做错了什么吗?为什么即使我是访客也无法访问 /posts/{id}? "Trying to get property of non-object" 消息是什么意思?
请告诉我是否应该在这里引用一些代码来帮助解决问题。
如有任何建议,我们将不胜感激。谢谢。
您在下一行收到错误
@if ( $post->user_id == Auth::user()->id)
在视图中。
您在此行收到错误的原因是您正试图像这样访问经过身份验证的用户的 ID,Auth::user()->id
。但是没有经过身份验证的用户。因此,Auth::user()
调用returnsnull
。您正在尝试在 null
上访问 id
。
尝试将其更改为
@if ( $post->user_id == @Auth::user()->id)
或
@if(Auth::user())
@if ( $post->user_id == Auth::user()->id)
...
@endif
@endif
大家好,只是想问一下Laravel像我这样学习的学生
登录后,我可以访问此页面:
Route::get('/posts/{id}', 'PostController@showById');
当我注销并尝试再次访问该页面时,我收到了提到的错误,我希望甚至为访客显示 posts,但不允许编写 post 如果他们已注销。
这里是 showById():
public function showById($id)
{
$post = Post::find($id);
return view('show-solo', compact('post'));
}
还有节目-solo.blade.php
@extends('master')
@include('partials.nav-none')
@section('content')
<div class="col-sm-8 blog-main">
<div class="blog-post">
@if ($flash = session('message'))
<div class="alert alert-success flash-message" role="alert">
{{ $flash }}
</div>
@endif
@if ( $post->user_id == Auth::user()->id)
<a href="/posts/{{ $post->id }}/delete">
<button class="btn-sm btn-warning post-btn" data-toggle="tooltip" data-placement="top" title="Delete Post"><i class="fa fa-times"></i></button>
</a>
<a href="/posts/{{ $post->id }}/edit">
<button class="btn-sm btn-primary post-btn" data-toggle="tooltip" data-placement="top" title="Edit Post"><i class="fa fa-pencil-square-o"></i></button>
</a>
@endif
<h2>Post number: {{ $post->id }}</h2>
<h2 class="blog-post-title">
<a class="title-link" href="/posts/{{ $post->id }}">{{ $post->title }}</a>
</h2>
<!-- {{ $post->created_at->toFormattedDateString() }} -->
<p class="blog-post-meta">{{ $post->created_at->diffForHumans() }} by <a href="#">{{ $post->user->name }}</a></p>
{{ $post->body }}
<hr />
@include('partials.error')
@include('partials.post-comment')
</div>
</div>
@endsection
如果这有帮助,这是我的路线:
Route::get('/', function () {
return view('welcome');
});
Route::get('/posts', 'PostController@index')->name('home');
Route::get('/posts/create', 'PostController@showForm');
Route::get('/posts/{id}', 'PostController@showById');
Route::get('posts/{id}/edit', 'PostController@editPostForm');
Route::get('posts/{id}/delete', 'PostController@deletePost');
Route::post('/posts', 'PostController@store');
Route::post('/posts/{post}/comments', 'CommentController@store');
Route::get('posts/{id}/delete', 'CommentController@deleteComment');
Route::post('/save-post', 'PostController@savePost');
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/register-user', 'RegistrationController@create');
Route::post('/register-user', 'RegistrationController@store');
Route::get('/login-user', ['as' => '/login-user', 'uses' => 'SessionController@create']);
Route::post('/login-user', 'SessionController@store');
Route::get('/logout-user', 'SessionController@destroy');
我做错了什么吗?为什么即使我是访客也无法访问 /posts/{id}? "Trying to get property of non-object" 消息是什么意思?
请告诉我是否应该在这里引用一些代码来帮助解决问题。
如有任何建议,我们将不胜感激。谢谢。
您在下一行收到错误
@if ( $post->user_id == Auth::user()->id)
在视图中。
您在此行收到错误的原因是您正试图像这样访问经过身份验证的用户的 ID,Auth::user()->id
。但是没有经过身份验证的用户。因此,Auth::user()
调用returnsnull
。您正在尝试在 null
上访问 id
。
尝试将其更改为
@if ( $post->user_id == @Auth::user()->id)
或
@if(Auth::user())
@if ( $post->user_id == Auth::user()->id)
...
@endif
@endif