从数据库中检索数据时出错

时间:2017-09-19 10:17:46

标签: php database laravel laravel-5

我试图用它的评论显示新闻,但其中一个标题是给我一个错误,但其他人正在工作,我有一张新闻表,我有一个news_comments表,一些新闻被检索完美的评论,但这一个给我一个错误。

enter image description here

animation

2 个答案:

答案 0 :(得分:0)

当您要访问的属性不存在时会发生此错误,因此您应该检查属性isset

以下是示例

@if (!is_null($news_comment->user))
    <div class="well">
        <a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>
        {{$news_comment->comments}}<br/>
        {{$news_comment->created_at}}
    </div> 
@endif

@if (isset($news_comment->user->id) && isset($news_comment->user->name))
    <div class="well">
        <a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>
        {{$news_comment->comments}}<br/>
        {{$news_comment->created_at}}
    </div> 
@endif

答案 1 :(得分:0)

尝试使用()上的user

<a href="{{action('ProfileController@show', [$news_comment->user()->id, $news_comment->user()->name])}}">{!! $news_comment->user()->name!!}</a>

而不是:

<a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>