因此,在尝试获取创建帖子的用户的名称时,我一直收到此错误。
我的模特看起来像这样:
class User extends Authenticatable {
use Notifiable, CanResetPassword;
public function posts() {
return $this->hasMany('App\Post');
}
}
&安培;
class Post extends Model {
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
}
我尝试在我的视图中显示名称:
@foreach($posts as $post)
<tr>
<td>{{ $post->user->name }}</td>
</tr>
@endforeach
我的控制器看起来像这样:
public function getAdminIndex() {
$posts = Post::orderBy('id', 'desc')->get();
$comments = Comment::all();
$likes = Like::all();
$users = User::all();
return view('admin.index', ['posts' => $posts, 'comments' => $comments, 'likes' => $likes, 'users' => $users]);
}
有谁能告诉我我做错了什么?
非常感谢!
答案 0 :(得分:4)
这意味着并非所有帖子都有用户,所以请执行以下操作:
__bases__
或者:
InputStream file = this.getClass().getResourceAsStream("/movies.txt");
Scanner scanner = new Scanner(file);