Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中使用一对多关系时尝试获取非对象的属性_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 在Laravel中使用一对多关系时尝试获取非对象的属性

Php 在Laravel中使用一对多关系时尝试获取非对象的属性,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,因此,当我试图获取创建帖子的用户的姓名时,总是会出现这个错误 我的模型是这样的: class User extends Authenticatable { use Notifiable, CanResetPassword; public function posts() { return $this->hasMany('App\Post'); } } public function getAdminIndex() { $posts = P

因此,当我试图获取创建帖子的用户的姓名时,总是会出现这个错误

我的模型是这样的:

class User extends Authenticatable {
    use Notifiable, CanResetPassword;

    public function posts() {
        return $this->hasMany('App\Post');
    }
}
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]);
}
<td>{{ optional($post->user)->name }}</td>
&

我尝试通过在我的视图中执行以下操作来显示名称:

@foreach($posts as $post)
    <tr>
        <td>{{ $post->user->name }}</td>          
    </tr>
@endforeach
谁能告诉我我做错了什么


多谢各位

这意味着并非所有帖子都有用户,所以请执行以下操作:

class User extends Authenticatable {
    use Notifiable, CanResetPassword;

    public function posts() {
        return $this->hasMany('App\Post');
    }
}
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]);
}
<td>{{ optional($post->user)->name }}</td>
{{可选($post->user)->name}
或:

{{empty($post->user)“'No user':$post->user->name}

这意味着并非所有帖子都有用户,所以请执行以下操作:

class User extends Authenticatable {
    use Notifiable, CanResetPassword;

    public function posts() {
        return $this->hasMany('App\Post');
    }
}
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]);
}
<td>{{ optional($post->user)->name }}</td>
{{可选($post->user)->name}
或:

{{empty($post->user)“'No user':$post->user->name}

非常感谢你,我成功了,我会尽快接受你的回答!非常感谢你,任选做了这个把戏,我会尽快接受你的回答!