0
因此,有三个表:Laravel 5.4获得第三个表信息雄辩关系
Schema::create('files_urls', function (Blueprint $table) {
\t $table->increments('id');
\t $table->integer('file_id')->unsigned();
\t $table->foreign('file_id')->references('id')->on('files');
\t $table->string('filename');
\t $table->timestamps();
});
public function up()
{
\t Schema::create('files', function (Blueprint $table) {
\t \t $table->increments('id');
\t \t $table->timestamp('date');
\t \t $table->string('name');
\t \t $table->string('description');
\t \t $table->integer('group_id');
\t \t $table->timestamps();
\t });
}
Schema::create('groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('course_id');
$table->timestamp('start_date')->nullable();
$table->timestamp('end_date')->nullable();
$table->timestamps();
});
着陆页控制器看起来是这样的:
public function listFiles($id){
\t \t $group = Group::find($id);
\t \t $data ['gInfo'] = $group;
\t \t $files = Group::find($id)->file;
\t \t $data ['fInfo'] = $files;
\t \t return view('upload.files', $data);
\t }
环在刀片文件:
@foreach ($fInfo as $file)
\t \t \t \t <tr>
\t \t \t \t \t <td>{{$file->date}}</td>
\t \t \t \t \t <td>{{$file->name}}</td>
\t \t \t \t \t <td>{{$file->description}}</td>
\t \t \t \t \t @foreach($file->file as $url)
\t \t \t \t \t \t {{$url->file->filename}}
\t \t \t \t \t @endforeach
\t \t \t \t </tr>
\t \t \t @endforeach
基本上,我想从文件打印的所有信息(可能的名字是不是就在这里 - 应该叫课程或东西)。但是,1节课(文件)可以有几个名称(来自files_urls表)。所以它应该在每个课程(文件)的一行中的日期,名称,描述和它在files_urls表中的所有名称打印一行。
的关系是这样的:
class FilesUrl extends Model
{
\t protected $fillable = ['file_id', 'filename'];
\t
\t public function file()
\t {
\t \t return $this->belongsTo('App\File');
\t }
}
public function file(){
\t \t return $this->hasMany('App\File'); \t
\t }
预先感谢您。
完美地工作。谢谢你,先生。还有很长的路要走:) – Benua