php - 拉拉维尔 : Property [name] does not exist on this collection instance

标签 php html laravel laravel-5 error-handling

我正在尝试从我的用户表中获取一些数据,用于我的个人资料页面。但是当我点击个人资料页面时,它给了我一些错误:

Property [name] does not exist on this collection instance.

问题出在哪里?是我做错了还是缺少一些代码?

用户模型

protected $fillable = [
    'name', 'email', 'password', 'nim', 'phone_number', 'department'
];

protected $hidden = [
    'password', 'remember_token',
];

public function dropcv(){
    return $this->hasMany('App\Dropcv');
}

public function registration(){
    return $this->hasMany('App\Registration');
}

Controller

public function index()
{
    $users = User::all();
    return view('users.profile')->with('users',$users);
}

public function show($id)
{
    $users = User::find($id);
    return view('users.profile')->with('users',$users);
}

查看

<h3>{{$users->name}}</h3>

在 View 部分,我只是做了一些测试以从用户表中获取简单数据。

我对找到问题的解决方案感到非常沮丧。我在网上搜索并尝试了很多解决方案,但没有任何效果。我对 Laravel 还很陌生,非常感谢一些帮助。

最佳答案

用于 Controller

public function show($id){
$users = User::find($id);
return view('users.profile',compact('users'));
}

用于查看页面

<h3>$users->name</h3>

用于 Controller

public function index(){
$users = User::all();
return view('users.profile',compact('users'));
}

您正在尝试访问数组上的属性,但该属性不正确 你应该先 foreach 然后访问你的属性(property)

@foreach($users as $user)
<h3>{{ $user->name }}</h3>
@endforeach

关于php - 拉拉维尔 : Property [name] does not exist on this collection instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56815592/

相关文章:

javascript - 如何将变量传递给 bootstrap 模式表单并运行 mysql 查询

PHP session 要使用文件系统与 Node.js 共享吗?

html - Bootstrap 4 导航栏理解

html - 在创建特定的固定 CSS 网格布局方面寻求帮助

php - Laravel - 更新模型时更新另一个属性

javascript - PHP错误消息未在浏览器中显示为JavaScript代码中间

php - PHP解析/语法错误;以及如何解决它们

html - 如何在悬停时使按钮内的文本透明?背景应该保持不变

php - 在 Laravel 上本地化日期的最佳方法是什么?

laravel - 如何在 Laravel Eloquent 中指定我的列不能为空?