ilkermutlu / laravel-gloss
此包已被弃用且不再维护。未建议任何替代包。
此包是创建类似词典视图的辅助工具。
dev-master
2016-05-13 07:50 UTC
Requires
- php: >=5.4.0
- illuminate/support: 5.*
This package is not auto-updated.
Last update: 2020-06-12 18:42:31 UTC
README
一个简单的Laravel5包,用于创建“类似词典”的视图。
许多人可能会觉得这个包是“微不足道”的,但我在创建“类似词典”的视图方面非常频繁,所以我决定推出这个包作为一个小助手。
这个小包添加了“Gloss” Laravel 门面,它接受一个数组或查询结果的集合,并按指定标签字段的第一个字母(默认为‘name’)对它们进行分组,以便在视图中使用。
希望在下一次更新中提供符合Bootstrap规范的模板生成。
欢迎所有想法、贡献和批评。
安装
composer require ilkermutlu/laravel-gloss
在config/app.php中,将以下行添加到providers
数组中
'Ilkermutlu\Gloss\GlossServiceProvider'
并将以下行添加到aliases
数组中
'Gloss' => 'Ilkermutlu\Gloss\GlossFacade'
使用方法
在脚本中导入门面
use Gloss;
你通常会使用它来创建一组单词的词典,但让我们考虑另一个场景:你可能希望有一个按字母分组的联系页面。
假设你有一个包含full_name
列的person
表。
// Get the records $records = \DB::select(\DB::raw('SELECT id, full_name FROM person')); // Create the glossary data set $gloss = Gloss::createFrom($records); // Set the name attribute $gloss->setName('full_name'); // Make the dataset $people = $gloss->make();
或者,你可以链接方法
$gloss = Gloss::createFrom($records)->setName('full_name')->make();
这将为你提供一个数组,该数组包含了你提供的按记录的不同第一个字母分组的查询结果集。
待办事项:(我将在有空的时候添加自动生成符合Bootstrap规范的视图的功能,你可以在我之前做这件事并创建一个拉取请求!)
在此之前;您可以在视图中使用这些数据,就像使用任何数据数组一样。
@foreach ($people as $letter => $person) <a href="#yourlinkhere">{{ $letter }}</a> @endforeach @foreach ($people as $letter => $person) <h1>{{ $letter }}</h1> <a href="/yourlinkhere/{{ $person->id }}">{{ $person->full_name }}</a> @endforeach