Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 为什么我能';t使用laravel 5.5中的一对多关系检索数据_Php_Laravel_Eloquent_Laravel 5.5 - Fatal编程技术网

Php 为什么我能';t使用laravel 5.5中的一对多关系检索数据

Php 为什么我能';t使用laravel 5.5中的一对多关系检索数据,php,laravel,eloquent,laravel-5.5,Php,Laravel,Eloquent,Laravel 5.5,我试图从我的数据库返回数据,我希望它包括来自相关表的数据。这是一种一对多的关系。然而,我得到的只是一个错误 此集合实例上不存在属性[User] 在我的用户模型中,我有 //App\User.php class User extends Authenticatable{ use Notifiable; public function systems(){ return $this->hasMany(\App\Models\General\systems::c

我试图从我的数据库返回数据,我希望它包括来自相关表的数据。这是一种一对多的关系。然而,我得到的只是一个错误

此集合实例上不存在属性[User]

在我的用户模型中,我有

//App\User.php
class User extends Authenticatable{
    use Notifiable;

    public function systems(){
        return $this->hasMany(\App\Models\General\systems::class,'added_by','id');
   }
另一种模式,叫做“我拥有的系统”

//App\Models\General\systems.php
class systems extends Model
{
    public function User(){
        return $this->belongsTo(\App\User::class,'added_by','id');
}
我的控制器里有

$this->systemsObject = new \App\Model\General\systems();

$systems = $this->systemsObject->get()->User;
根据Laravel的文档,这应该是可行的,但事实并非如此。我尝试反转外键/本地键参数。我将->用户设置为大写,小写


我不知道我做错了什么

您需要迭代,例如:

$systems = $this->systemsObject->get();
foreach ($systems as $system) {
    echo $system->User->name;
}