【发布时间】: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,一直滚动到底部以查找您的最新错误。