doukong5394 2017-06-06 13: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 13: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
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部