laravel - 如何在刀片中执行嵌套的 foreach 循环?
问题描述
我想显示 Laravel Blade 中的主题以及该主题下每个主题中教授的相关主题。我怎么能在 Laravel 8 中做到这一点。
这是我的模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class topic extends Model
{
use HasFactory;
protected $table='topics';
protected $primaryKey='id';
protected $fillable=['author_id', 'course_id', 'url', 'topic', 'powerpoint', 'description', 'views'];
public function author(){
return $this->belongsTo(author::class);
}
public function course(){
return $this->belongsTo(course::class);
}
}
这是我的控制器:
public function courses(){
$topics = topic::join('authors', 'topics.author_id', '=', 'authors.id')
->join('courses', 'topics.course_id', '=', 'courses.id')
->select('topics.*', 'authors.first_name', 'authors.last_name', 'courses.course')
->get();
$topicsCounter = topic::join('authors', 'topics.author_id', '=', 'authors.id')
->join('courses', 'topics.course_id', '=', 'courses.id')
->count();
return view('courses', compact('topics', 'topicsCounter'));
}
这是我的刀片:
@foreach($topics as $topic)
{{ $topic->course }} :
//here is where i want to list all the topic found in this specific course
@endforeach
这是正确的方式还是我应该以另一种更好的方式来做?
解决方案
使用外部组件接受 $course 作为参数,然后将其包含在主刀片中。
推荐阅读
- powershell - 如何从 PowerShell 的目录输出中删除 FullName 标头
- react-native - 有没有办法在 React Native 中更改 DialogInput 中的键盘类型?
- r - R找到数据框列中最大值的索引
- javascript - JS中使用formdata和fetchAPI将表单上传的图片发送到服务器
- math - 表中值的插值
- java - 如何在控制器的方法中注入服务?
- sparse-matrix - 稀疏和复杂的广义特征值问题
- python - 如何使用 GOOGLE 作为 pandas 数据阅读器的数据源?
- bash - Git推送并行进程失败
- python - 根据条件将 Pandas 列中的文本拆分为不同的列