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>