祝大家今天好,我只想问一下拉勒维尔的情况,让我学习像我这样的学生。
在登录时,我可以访问以下页面:
Route::get('/posts/{id}', 'PostController@showById');当我注销并尝试再次访问该页面时,我会看到提到的错误,我希望为来宾显示帖子,但如果他们被注销,则不允许撰写帖子。
下面是showById():
public function showById($id)
{
$post = Post::find($id);
return view('show-solo', compact('post'));
}还有显示-solo.blde.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');我做错什么了吗?为什么即使我是客人,也不能访问/发布/{id}?“试图获取非对象的属性”意味着什么?
请告诉我是否应该在这里引用一些代码来帮助解决这个问题。
如有任何建议,将不胜感激。谢谢。
发布于 2017-03-23 03:21:06
您将在下面的行中获得错误
@if ( $post->user_id == Auth::user()->id)在视野中。
您在这一行上得到错误的原因是您试图访问像Auth::user()->id这样的经过身份验证的用户的id。但是没有经过身份验证的用户。因此,Auth::user()调用返回null。您正在尝试访问id on null。
试着把它改成
@if ( $post->user_id == @Auth::user()->id)或
@if(Auth::user())
@if ( $post->user_id == Auth::user()->id)
...
@endif
@endifhttps://stackoverflow.com/questions/42966637
复制相似问题