【问题标题】:Trying to get property of non-object Laravel [duplicate]试图获取非对象 Laravel 的属性 [重复]
【发布时间】:2017-10-04 11:36:58
【问题描述】:

所以,我创建了一个控制器来在我的应用程序中添加主题,并将其存储在数据库中。唯一的问题是当我显示一个视图时,它给了我 ErrorException。

这里是主题控制器

 public function store(Request $request)
{
    $users = User::all();

    $userid = Auth::user()->id;

    $topic = new Topic([
      'user_id' => $userid,
      'title' => $request->get('title'),
      'description' => $request->get('description'),
      'tags' => $request->get('tags')
    ]);

    $topic->save();


    //Creating a loop for each user, send a notification.
     $message_content = "The " . Auth::user()->first_name . " " . Auth::user()->last_name . " just added a new topic. \n " . $topic->title . " \n ";

        foreach ( $users as $user ) {

            mail( $user->email, 'New Topic!', $message_content);
        }

    return redirect('/topics')->with('topics', $topics);



}

这里是风景

@foreach($topics as $topic)
    <div class="col-lg-4 col-md-6">
      <div class="widget widget-shadow">
        <div class="widget-content padding-20 bg-green-500 white height-full">

          <a class="avatar pull-left margin-right-20" href="javascript:void(0)">
            <img src=" {{URL::to('uploads')}}/{{ $topic->creator->image }} " alt=" ">
          </a>
          <div style="overflow:hidden;">
            <small class="pull-right grey-200">{{$topic['created_at']}}</small>
            <div class="font-size-18"> {{$topic->creator->first_name}}  {{$topic->creator->last_name}}</div>
            <div class="grey-200 font-size-14 margin-bottom-10">{{$topic->creator->role}}</div>
            <blockquote class="cover-quote font-size-16 white">{{$topic['title']}}
            </blockquote>
          </div>

        </div>
      </div>
    </div>
    @endforeach

这里是主题模型

class Topic extends Model

public $fillable = ['title', 'user_id', 'description', 'tags'];

public function creator() {
    return $this->belongsTo( User::class, 'user_id', 'id');

这里是用户模型(仅与主题模型有关)

public function hasTopic() {
    return $this->hasMany( Topic::class, 'id', 'user_id' );
}

最后是获取主题数据并显示视图的控制器

public function sdp()
{

    $topics = Topic::all();

    $users = User::with([ 'hasTopic' => function ($query) {
        $query->where('id', 'user_id');

    }]);


    return view('topics.student', ['topics' => $topics, 'users' => $users] );

}

【问题讨论】:

  • 提供你得到@ffs33的错误
  • [01:44:39] LOG.error: ErrorException: Trying to get property of non-object in
  • 我刚刚检查了我的本地主机它可以显示视图但在服务器上它不会工作
  • 你的$topics的数据格式是什么?
  • 您查看您的日志文件了吗?它位于storage/logs/laravel.log,一直滚动到底部以查找您的最新错误。

标签: php laravel


【解决方案1】:

您没有通过重定向传递 $topic 变量

return redirect('/topics')->with('topics', $topics);

更新 1: 可以这样试试吗

$topic->created_at

而不是像 $topic 中的对象那样使用类似数组,而不是数组。

$topic['created_at']

Read more

【讨论】:

  • 仍然出现同样的错误
  • 那么你如何将$topics 变量传递给你的视图,在这个控制器中你只需重定向到/topics 路由。您将哪个控制器和功能用于/topics 路由?错误必须在那里生成。
  • 我刚刚编辑了我的问题,你可以检查上面你会看到视图
  • 提到我可以看到本地主机上发布的主题,但是在服务器上显示该错误
  • 你能正确分享你的例外吗?找不到哪个对象?行号,文件名?
【解决方案2】:

由于错误表明您正在尝试获取空对象的属性。

我不清楚它是什么对象,因为我没有看到堆栈跟踪或文件的内容。但作为它的视图(/home/118696.cloudwaysapps.com/fwzyssdrmj/public_html/storag‌​‌​e/framework/views/‌​7b‌​fce097aa53fc730f‌​134b‌​425d87dd73f09b‌​d893.p‌​hp)我假设可能是主题的创建者设置为 null,但您正在尝试访问该 null 对象的属性。假设您使用 user_id 保存主题时假设用户已登录并且有一个 id 但它为空。稍后您将尝试访问作为主题的 user_id 的空对象 ($topic->creator)。检查该字段是否可以为空?如果是,请检查是否存在 user_id 为 null 的主题。

【讨论】:

  • 不,它正确获取数据库中的user_id
  • 你能告诉我 storag‌​‌​e/framework/views/‌​7b‌​fce097aa53fc730f‌​134b‌​425d87dd73f09b‌​d893.p‌​hp 的内容,以便我可以立即告诉您出了什么问题
猜你喜欢
  • 1970-01-01
  • 2016-07-16
  • 2015-09-22
  • 2017-03-20
  • 2014-03-25
  • 2015-01-25
  • 2014-06-23
  • 1970-01-01
相关资源
最近更新 更多