laravel 过滤laravel中的数据并处理视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27967437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filtering data in laravel and handling the views
提问by Ortix92
I have a tv show netflix-esque project I'm building where I have a Shows
page which I want to filter on format. Each show contains episodes which can have a tv, dvd and bd format.
我有一个电视节目 netflix-esque 项目,我正在构建一个Shows
我想按格式过滤的页面。每个节目都包含可以有 tv、dvd 和 bd 格式的剧集。
Currently I'm filtering using separate routes and controllers which extend the base ShowsController
.
目前,我正在使用扩展 base 的单独路由和控制器进行过滤ShowsController
。
Route::get('shows/view/{type}', ['as' => 'shows.viewtype', 'uses' => 'ShowsController@viewType',]);
Route::get('shows/bluray',['as' => 'shows.bluray','uses' => 'ShowsBlurayController@index']);
Route::get('shows/dvd',['as' => 'shows.dvd','uses' => 'ShowsDVDController@index']);
Route::get('shows/tv',['as' => 'shows.tv','uses' => 'ShowsTVController@index']);
Example of one of the format controllers
格式控制器之一的示例
class ShowsBlurayController extends ShowsController
{
public function index()
{
// Set user state for browsing bluray
Session::push('user.showtype', 'bluray');
$shows = $this->show->getBlurayPaginated(16);
return $this->getIndexView(compact('shows'));
}
}
I use the getIndexView()
method (in the ShowsController
) to determine one of 2 available views: poster
and list
.
我使用getIndexView()
方法(在 中ShowsController
)来确定 2 个可用视图之一:poster
和list
。
public function getIndexView($shows)
{
$viewType = get_session_or_cookie('show_viewtype', 'list');
if ($viewType == 'posters') {
return View::make('shows.index', $shows)
->nest('showsView', 'shows.partials.posters', $shows);
} else {
return View::make('shows.index', $shows)
->nest('showsView', 'shows.partials.list', $shows);
}
}
The shows are filtered based on the episodes:
这些节目是根据剧集过滤的:
public function getBlurayPaginated($perPage)
{
return $this->getByFormat('BD')->with('tagged')->paginate($perPage);
}
private function getByFormat($format)
{
return $this->show->whereHas('episodes', function ($q) use ($format) {
$q->whereHas('format', function ($q) use ($format) {
$q->where('format', '=', $format);
});
});
}
The problem is that I want to do this in a clean way. When a user selects a format, that filter will be applied. Currently all of this is kind of scattered across controllers and doesn't quite make sense.
问题是我想以一种干净的方式做到这一点。当用户选择一种格式时,将应用该过滤器。目前,所有这些都分散在控制器中,并没有多大意义。
I also thought of doing something like this in the routes.php
:
我也想过在以下内容中做这样的事情routes.php
:
Route::get('shows/format/{format}',['as' => 'shows.format','uses' => 'ShowsController@index']);
And then handle the filtering in the index, but that also seems a weird place to do that.
然后处理索引中的过滤,但这似乎也是一个奇怪的地方。
This approach does work, but I don't want to screw myself later on with it. I'm planning a simple search which should take the filter into account.
这种方法确实有效,但我不想以后再用它来搞砸自己。我正在计划一个应该考虑过滤器的简单搜索。
In other words, how can I organize the code in such a way that getting data from the database will take the filter into account which has been set? (Session states maybe?)
换句话说,我如何组织代码,以便从数据库中获取数据将考虑已设置的过滤器?(会话状态可能?)
采纳答案by Jeff Lambert
Route::get('shows/format/{format}',[
'as' => 'shows.format',
'uses' => 'ShowsController@index'
]);
I think you're on the right track here. I would go so far as to produce a factoryand inject it into the controller. The purpose of this factory is to construct a formatter that will supply your view with the correct data:
我认为你在这里走在正确的轨道上。我什至会生产一个工厂并将其注入控制器。这个工厂的目的是构建一个格式化程序,它将为您的视图提供正确的数据:
// ShowController
public function __construct(ShowFormatFactory $factory, ShowRepository $shows)
{
$this->factory = $factory;
// NB: using a repository here just for illustrative purposes.
$this->shows = $shows;
}
public function index($format = null)
{
$formatter = $this->factory->make($format);
return View::make('shows.index', [
'formatter' => $formatter,
'shows' => $this->shows->all(),
]);
}
// ShowFormatFactory
class ShowFormatFactory
{
public function make($format)
{
switch($format) {
case 'blueray':
return new BluerayFormat(); break;
case 'dvd': /* Fallthrough for default option */
default:
return new BluerayFormat(); break;
}
}
}
// ShowFormatInterface
interface ShowFormatInterface
{
public function format(Show $show);
}
// BluerayFormat
class BluerayFormat implements ShowFormatInterface
{
public function format(Show $show)
{
return $show->blueray_format;
}
}
Then in your view, since you are guaranteed to have an object that will provide you the format requested for a given show, just call it:
然后在您看来,由于您可以保证有一个对象可以为您提供给定节目所需的格式,只需调用它:
@foreach($shows as $show)
<div class="show">
Chosen Format: {{ $formatter->format($show) }}
</div>
@endforeach
This solution is testable and extensible will allow you to add other formats later on. If you do, you would need to add a discrete case
statement in the factory for each different format, as well as write a rather slim ~5-7 line class to support the new format.
此解决方案可测试且可扩展,允许您稍后添加其他格式。如果这样做,您需要case
在工厂中为每种不同的格式添加一个离散语句,并编写一个相当精简的 ~5-7 行类来支持新格式。