doukong5394 2017-06-06 21:02
浏览 36
已采纳

Laravel 5.4获得第三表信息雄辩的关系

So there are three tables:

Schema::create('files_urls', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('file_id')->unsigned();
            $table->foreign('file_id')->references('id')->on('files');
            $table->string('filename');
            $table->timestamps();
        });

public function up()
    {
        Schema::create('files', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamp('date');
            $table->string('name');
            $table->string('description');
            $table->integer('group_id');
            $table->timestamps();
        });
    }

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();
        });

Landing page controller looks like this:

public function listFiles($id){
        $group = Group::find($id);
        $data ['gInfo'] = $group;
        $files = Group::find($id)->file;
        $data ['fInfo'] = $files;
        return view('upload.files', $data);
    }

loop in the blade file:

@foreach ($fInfo as $file)
                <tr>
                    <td>{{$file->date}}</td>
                    <td>{{$file->name}}</td>
                    <td>{{$file->description}}</td>
                    @foreach($file->file as $url)
                        {{$url->file->filename}}
                    @endforeach
                </tr>
            @endforeach

Basically, i want to print all information from the file(probably the name is not right here - should be called lesson or something). However, 1 lesson (file) can have a few names (from files_urls table). So it should print date, name, description and all names that it has in the files_urls table in one row for each lesson(file).

relationships are like this:

class FilesUrl extends Model
{
    protected $fillable = ['file_id', 'filename'];
    
    public function file()
    {
        return $this->belongsTo('App\File');
    }
}

public function file(){
        return $this->hasMany('App\File');   
    }

Thank you in advance.

</div>
  • 写回答

1条回答 默认 最新

  • duandie0921 2017-06-06 21:52
    关注

    It doesn't look as though your model relationships are set up correctly, or that you're accessing them correctly.

    You can also make some efficiencies in your controller by not querying for the Group model twice.

    I have assumed from your database schema that a Group has many Files and a File has many FilesUrls.

    <?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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀