Reputation: 269
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.
Upvotes: 0
Views: 604
Reputation: 17388
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 File
s and a File
has many FilesUrl
s.
<?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
Upvotes: 1