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