2017-06-06 105 views
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 }

預先感謝您。

回答

1

它看起來好像您的模型關係設置正確,或者您正確訪問它們。

您也可以通過不查詢Group模型兩次,在控制器中提高效率。

我從一個Group有許多File是你的數據庫架構假設和File有許多FilesUrl秒。

<?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 
+0

完美地工作。謝謝你,先生。還有很長的路要走:) – Benua