【发布时间】:2020-05-19 15:13:54
【问题描述】:
我遇到了一些循环问题。
我有 2 个表 Terms [id, caption, ...] 和 apps [id, term_id, name, ...]
在我看来,我想打印所有 apps 按 term_id 然后按名称排序。但我希望 apps 中使用的每个 term_id 都有 1 个表。我希望第一个列具有正确的 $loop->iteration(我的意思是每个表的第一行从 1 开始,而不是从上一个表的最后一行开始 +1)
在控制器中,我将 2 个变量传递给我的视图(每个 appState_id 值我都有不同的视图)
$app = Application::where('ApplicationState_id',3)->get();
$term = Terms::all();
return view('admin.index')->with('terms', $term)
->with('apps', $app);
在视图中我在 foreach 中使用了 foreach / 我稍微重建了它
foreach terms as term
foreach apps as app
if $loop->first {<table><thead> ... table caption row ...}
checking if app->term_id = term->id
$loop->iteration, and some other stuff
if $loop->last {</table>}
endforeach
endforeach
但我希望它改进一点:
即使应用表中没有记录,它也会为所有术语写入表的第一个/标题/行
$loop->iteration 按所有记录组合 我希望每张桌子都单独使用它-我不知道我能做到吗
如何将控制器中的所有数据传递给能够将它们分别打印到对应表?
有没有更好的方法来做到这一点?感谢您的建议/提示
---编辑了一点点缩短
Application.php
class Application extends Model
{
protected $fillable = [
'nickname',
'comment',
'term_id',
'applicationstate_id',
];
public function term()
{
return $this->belongsTo('App\Term', 'Term_id');
}
}
Term.php - 这个模型/表格是由我自己填写的,但我想在未来改变用户可以添加其他事件的条款
class Term extends Model
{
public function applications()
{
return $this->hasMany('App\Application');
}
}
我知道我可以在控制器中:
- 检查应用程序表 {n} 中有多少唯一 term_id
- 循环 1... {n}
内部循环从数据库中为每个查询/每个 term_id 读取数据并将结果添加到数组中
将此结果数组发送到查看
然后尝试将其打印为每个 term_id 的 {n} 个表
这是如何解决这个问题的好主意,还是有更好的方法?
表格应用程序
data from applications table/few records/
页面上的理想输出 ideal output scheme
【问题讨论】:
标签: laravel eloquent laravel-blade