我有 3 個表格、表格課程、表格部分和表格教學大綱。其中表格課程與表格部分相關,表格部分與表格教學大綱相關。為了在我的表格下方清楚說明,如下所示:
#Table 課程:
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->unsignedBigInteger('category_id')->default('1');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade')->onUpdate('cascade');
$table->string('title');
$table->string('status')->default('publish');
$table->text('content')->nullable();
$table->string('level');
$table->date('start_at');
$table->date('ended_at');
$table->integer('capacity');
$table->text('image')->nullable();
$table->text('url')->nullable();
$table->text('requirement')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
表格部分:
public function up()
{
Schema::create('sections', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('course_id');
$table->foreign('course_id')->references('id')->on('courses');
$table->string('name');
$table->timestamps();
});
}
和最后一個表格教學大綱:
public function up()
{
Schema::create('syllabuses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('section_id');
$table->foreign('section_id')->references('id')->on('sections')->onDelete('cascade')->onUpdate('cascade');
$table->string('title')->nullable();
$table->text('content')->nullable();
$table->text('description')->nullable();
$table->string('total_of_sessions')->nullable();
$table->string('durations')->nullable();
$table->timestamps();
});
}
以下是該表的每個模型:
#課程模型
class Course extends Model
{
use HasFactory,SoftDeletes;
protected $fillable = [
'title',
'content',
'level',
'start_at',
'ended_at',
'capacity',
'image',
'url',
'requirement'
];
protected $dates = ['deleted_at'];
public function user()
{
return $this->belongsTo(User::class);
}
public function sections()
{
return $this->hasMany(Section::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
}
#截面模型
class Section extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function syllabuses()
{
return $this->hasMany(Syllabus::class);
}
public function course()
{
return $this->belongsTo(Course::class);
}
}
#教學大綱模型
class Syllabus extends Model
{
use HasFactory;
public $table = 'syllabuses';
protected $fillable = [
'title',
'content',
'description',
'total_of_sessions',
'durations'
];
public function section()
{
return $this->belongsTo(Section::class);
}
}
以下是我的路由和控制器:
//Route
Route::get('/course/{course}', [App\Http\Controllers\HomeController::class, 'showcourse'])->name('course');
//Display single course page
public function showcourse(Request $request)
{
$course = Course::with('sections', 'sections.syllabuses')->where('id', $request->route('course'))->first(); ->Your code I have paste
return view('course-single', [
'syllabuses' => $syllabuses
]);
}
最后是我的刀片:
@foreach($course->sections as $section)
<li class="list-group-item p-0 bg-transparent">
<!-- Toggle -->
<a class="h4 mb-0 d-flex align-items-center text-inherit text-decoration-none py-3 px-4"
data-bs-toggle="collapse" href="#course{{ $section->id }}" role="button" aria-expanded="false"
aria-controls="course{{ $section->id }}">
<div class="me-auto">
<!-- Title -->
{{ $section->name }}
<p class="mb-0 text-muted fs-6 mt-1 fw-normal">1/2 hari (2 sesi)</p>
</div>
<!-- Chevron -->
<span class="chevron-arrow ms-4">
<i class="fe fe-chevron-down fs-4"></i>
</span>
</a>
<!-- Row -->
<!-- Collapse -->
<input type="text" value="{{ $section->id }}" name="idsection">
<div class="collapse" id="course{{ $section->id }}" data-bs-parent="#courseAccordion">
@foreach($course->sections as $section)
@foreach($section->syllabuses as $syllabus)
<div class="px-4 pt-0 pb-2">
<div class="d-flex justify-content-between align-items-center text-inherit">
<div class="text-lg-start">
<span class="icon-shape bg-light text-secondary icon-sm rounded-circle me-2">
<i class="fe fe-check fs-4"></i>
</span>
</div>
<div class="col lh-1 ">
<span>{{ $syllabus->title }}</span>
</div>
<div class="text-left px-1">
<span>2s 180m</span>
</div>
</div>
</div>
@endforeach
@endforeach
</div>
</li>
@endforeach
現在我不得不根據 Sections 的 ID 來顯示表格教學大綱中的資料,而這些部分是根據 Courses 的 ID 顯示的,因為我不知道如何從與 Sections 的 ID 相關的每一行中獲取要放入的 ID Syllabus::where('section_id',???)->get();。任何能幫助我的人都非常感激。希望你明白我的意思。
在你可以看到的圖片下方,我已經做了一些注釋讓你理解。綠框問題之一。
在此處輸入圖片說明
uj5u.com熱心網友回復:
你需要研究你的 laravel 關系,你有屬于你的關系。
在您的教學大綱模型中添加。
public function section(){
return $this->belongsTo(Section::class, 'section_id');
}
然后你可以使用它來獲得所有與這些部分相關的教學大綱。
Syllabus::with('section')->get();
uj5u.com熱心網友回復:
首先在模型中定義關系。我假設您的模型是:課程、部分和教學大綱
// Model Course
public function sections()
{
return $this->hasMany(Section::class);
}
// Model Section
public function course()
{
return $this->belongsTo(Course::class);
}
public function syllabuses()
{
return $this->hasMany(Syllabus::class);
}
// Model Syllabus
public function section()
{
return $this->belongsTo(Section::class);
}
現在在您的控制器中,您可以通過以下方式獲得指定的課程
// Controller Course
public function show($id)
{
$course = Course::with('sections', 'sections.syllabuses')->where('id', $id)->first();
}
在此查詢中,您有一個來自“Course”表的模型、來自“Sections”表的相關模型以及來自“Syllabuses”表的所有相關模型。如果您使用的是 laravel 的刀片,那么您可以通過以下方式簡單地訪問它們:
// course
{!! $course->your_attribute !!}
// to access sections
@foreach($course->sections as $section)
@endforeach
// to access syllabuses
@foreach($course->sections as $section)
@foreach($section->syllabuses as $syllabus)
// Here you will have syllabuses for individual sections
@endforeach
@endforeach
https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393631.html
