Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.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 eloquent访问多个外键_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 如何使用laravel eloquent访问多个外键

Php 如何使用laravel eloquent访问多个外键,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我想取一个学生的档案,里面有所有的数据 我的表格(我总结了我需要的表格): 型号: // ... class Student extends Authenticatable{ // ... public function files() { return $this->hasMany('App\StudentFiles'); } // ... 控制器: // ... class Stu

我想取一个学生的档案,里面有所有的数据

我的表格(我总结了我需要的表格):

型号:


    // ...
    class Student extends Authenticatable{
    // ...

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

    // ...

控制器:


    // ...
    class Student extends Authenticatable{
     public function getFiles(Request $request)
     {
        $user = Student::Find($request->user()->id)->load('files');
     }
    // ...

我试过这样的方法。。。不起作用


     public function files()
     {
        return $this->hasMany('App\StudentFiles')->hasOne('App\TypeFiles');
     }

请求的结果:


// ...

    "files": [
        {
            "id": 1,
            "student_id": 1,
            "type_file_id": 1,
            "file_url": "example.jpg",
        }
    ]

// ...

我想要的结果

 // ...


       "files": [
            {
                "id": 1,
                "student_id": 1,
                "type_file_id": 1,
                "name": "Profile",
                "file_url": "example.jpg",
            }
        ]
      // ...


我试过几种方法,但我无法解决我的问题。

您可以将
类型文件
关系添加到StudentFile:

class StudentFiles扩展模型{
公共函数类型文件()
{
返回$this->hasOne('App\TypeFile','type\u file\u id');
}
}
然后使用“快速加载”访问它:

$user=Student::with('files','files.typeFile')->find($request->user()->id);
$typeFileName=$user->files->typeFile->name;
 // ...


       "files": [
            {
                "id": 1,
                "student_id": 1,
                "type_file_id": 1,
                "name": "Profile",
                "file_url": "example.jpg",
            }
        ]
      // ...