Php Laravel 5.4获取第三个表格信息-雄辩的关系
因此有三个表: Schema::创建“文件\ URL”,函数蓝图$表{ $table->increments'id'; $table->integer'file_id'->无符号; $table->foreign'file_id'->引用'id'->在'files'上; $table->string'filename'; $table->timestamps; }; 公共职能 { Schema::创建“文件”,函数蓝图$表{ $table->increments'id'; $table->timestamp'date'; $table->string'name'; $table->string'description'; $table->integer'group_id'; $table->timestamps; }; } 架构::创建“组”,函数蓝图$表{ $table->increments'id'; $table->string'name'; $table->integer'course_id'; $table->timestamp'start_date'->可为空; $table->timestamp'end_date'->可为空; $table->timestamps;Php Laravel 5.4获取第三个表格信息-雄辩的关系,php,eloquent,relationship,laravel-5.4,Php,Eloquent,Relationship,Laravel 5.4,因此有三个表: Schema::创建“文件\ URL”,函数蓝图$表{ $table->increments'id'; $table->integer'file_id'->无符号; $table->foreign'file_id'->引用'id'->在'files'上; $table->string'filename'; $table->timestamps; }; 公共职能 { Schema::创建“文件”,函数蓝图$表{ $table->increments'id'; $table->ti
}; 看起来您的模型关系设置不正确,或者访问不正确 您还可以通过不两次查询组模型来提高控制器的效率 我根据您的数据库模式假设,一个组有许多文件,而一个文件有许多文件URL
工作完美。谢谢你,先生。对我来说还有很长的路要走:
<?php
// app/Group.php
namespace App;
class Group extends Model
{
// ...
public function files()
{
return $this->hasMany(App\File::class);
}
// ...
}
// app/File.php
namespace App;
class File extends Model
{
// ...
public function group()
{
return $this->belongsTo(App\Group::class);
}
public function urls()
{
return $this->hasMany(App\FilesUrl::class)
}
// ...
}
// app/FilesUrl.php
namespace App;
class FilesUrl extends Model
{
// ...
public function file()
{
return $this->belongsTo(App\File::class);
}
// ...
}
// app/Http/Controllers/LandingPageController.php
namespace App\Http\Controllers;
class LandingPageController extends Controller
{
// ...
public function listFiles($id)
{
$group = Group::with('files.urls')->findOrFail($id);
return view('upload.files', compact('group'));
}
// ...
}
// resources/views/upload/files.blade.php
@foreach ($group->files as $file)
<tr>
<td>{{$file->date}}</td>
<td>{{$file->name}}</td>
<td>{{$file->description}}</td>
<td>
@foreach($file->urls as $url)
{{$url->filename}}
@endforeach
</td>
</tr>
@endforeach
@endforeach