【问题标题】:error while retrieving data from the database从数据库中检索数据时出错
【发布时间】:2017-09-19 10:17:46
【问题描述】:

我试图用它的 cmets 显示新闻,但其中一个标题给了我一个错误,但其他人正在工作,我有一个新闻表,我有一个 news_cmets 表,正在检索一些新闻与他们的 cmets 完美搭配,但这个给了我一个错误。

@extends('layouts.app')


@section('content')
<div class="container">
  {{$news->title}}<br/>
  @foreach($news->news_pictures as $news_picture)
    <img src="{{asset($news_picture->pictures)}}" width="200"><br/>
    @endforeach
   {!! $news->body !!} <br/>
  <small>written on{{$news->created_at}} by {{$news->user->name}} </small>


</div>


@foreach($news->news_comments as $news_comment)

  @if(!Auth::guest())
    <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>
    @else
<div class="well">
  {{$news_comment->commentor}}<br/>
  {{$news_comment->comments}}<br/>
  {{$news_comment->created_at}}
</div>
    @endif

  @endforeach








@if(!Auth::guest())
  <form action="{{action('NewsController@AddComments',[$news->id])}}" method="post">
    {{csrf_field()}}
    <div class="container">
      <textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
      <button class="btn btn-primary" >post</button>
    </div>
  </form>


  @else
  <form action="{{action('NewsController@AddComments',[$news->id])}}" method="post">
    {{csrf_field()}}
    <div class="container">
      <input type="text" class="form-control" placeholder="your name" name="commentor">
      <textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
      <button class="btn btn-primary" >post</button>
    </div>
  </form>
  @endif
@endsection

【问题讨论】:

  • 你能打印 echo "
    ";打印_r($新闻);并发布结果
  • 为什么在链接中有控制器方法作为动作?你想在这里实现什么? &lt;a href="{{action('ProfileController@show', [$news_comment-&gt;user-&gt;id, $news_comment-&gt;user-&gt;name])}}"&gt;{!! $news_comment-&gt;user-&gt;name!!}&lt;/a&gt;
  • 此错误表明,您要访问的不存在的属性尝试调试它可能是null

标签: php database laravel laravel-5


【解决方案1】:

当您要访问的属性不存在时会发生此错误,因此您应该检查属性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

【讨论】:

    【解决方案2】:

    尝试使用:() on 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>
    

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多