2017-06-06 58 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