我在为广播新闻做作业。试图返回一个特定新闻节目的所有作业的视图,我就陷入了困境。
表
用户表
id | name
---|-------
1 | Admin
2 | Susan
3 | Ed
4 | Jen新闻广播台
id | forStation_id | name
---|---------------|-----
1 | 1 | AM
2 | 1 | PM
3 | 2 | Sports台站表
id | calls
---| -----
1 | JNDV
2 | YXWQ赋值表
id | anchorId | newscastId | startDate | endDate | isTemp
---|----------|------------|-------------|-------------|--------
1 | 2 | 1 | 01 May 2017 | 31 Dec 2999 |
2 | 3 | 1 | 02 May 2017 | 06 May 2017 | True
3 | 4 | 2 | 01 Apr 2017 | 31 Dec 2999 |
4 | 3 | 3 | 01 Apr 2017 | 28 Apr 2017 |(部分)分配模型
public function anchor()
{
return $this->belongsTo(User::class, 'anchor_id')->withTrashed();
}
public function cast()
{
return $this->belongsTo(Newscast::class, 'cast_id')->withTrashed();
}(部分) Newscast模型
public function for_station()
{
return $this->belongsTo(Station::class, 'for_station_id')->withTrashed();
}
function getNameInputStationAttribute() {
return $this->for_station->calls . "-" . $this->name_input;
}(部分)分配控制器
/**
* Display all Assignments for one input name.
*
* @param int $name
* @return \Illuminate\Http\Response
*/
public function showAssignmentsByCastName($castName)
{
if (! Gate::allows('assignment_view')) {
return abort(403);
}
$relations = [
'anchors' => \App\User::get()->pluck('name', 'id'),
'casts' => \App\Newscast::with('for_station')->get()->pluck('name_input_station', 'id'),
'assignments' => \App\Assignment::with('cast')->get(),
];
dump($relations);
return view('assign.list', compact('castName') +$relations);
}正如我所预期的,这段代码返回assignments的完整集合。
输出
Anchor | Cast Name | Start Date | End Date
---------|-------------|-------------|-------------
Susan | JNDV-AM | 01 May 2017 | 31 Dec 2999
Ed | JNDV-AM | 02 May 2017 | 06 May 2017
Jen | JNDV-PM | 01 Apr 2017 | 31 Dec 2999
Ed | YXWQ-Sports | 01 Apr 2017 | 28 Apr 2017我已经尝试过几种方法将作业限制在一个newscastId上,到目前为止都没有成功。
2017年5月1日/assignment/list/JNDV-AM 的预期产量
Anchor | Cast Name | Start Date | End Date
---------|-------------|-------------|-------------
Susan | JNDV-AM | 01 May 2017 | 31 Dec 2999
Ed | JNDV-AM | 02 May 2017 | 06 May 2017短期作业是暂时的(isTemp=True).在有效的日子里,它应该列在上面。
2017年5月2日至2017年5月6日的预期产出
Anchor | Cast Name | Start Date | End Date
---------|-------------|-------------|-------------
Ed | JNDV-AM | 02 May 2017 | 06 May 2017
Susan | JNDV-AM | 01 May 2017 | 31 Dec 2999我正在修改由管理面板生成器工具生成的代码。在我看来,向所有用户和所有类型查询并不是最有效的方法。我认为,由于我只是在为一个新闻节目寻找当前和未来的任务,所以anchors和casts关系应该被过滤。
基本问题我应该做什么改变
'assignments' => \App\Assignment::with('cast')->get(),
只获得JNDV (newscastId = 1)的赋值?
高级问题,您建议如何更改relations,使其只返回当前和未来的JNDV作业,即今天的作业,并且尽可能少的查询?
发布于 2017-05-02 03:42:21
下面是我想出的工作代码:
(部分)分配控制器
public function showAssignmentsByCastName($calls, $name_input)
{
if (!Gate::allows('assignment_view')) {
return abort(403);
}
$castName = strtoupper($calls). "-" . $name_input;
$station_id = \App\Station::where('calls', $calls)->get();
$station = \App\Station::findOrFail($station_id);
$cast_id = \App\Newscast::where([
['for_station_id', $station->id],
['name_input', $name_input]
])->get();
$cast = \App\Newscast::findOrFail($cast_id);
$relations = [
'anchors' => \App\User::get()->pluck('name', 'id'),
'casts' => \App\Newscast::where('cast_id', $cast->id),
'assignments' => \App\Assignment::where('cast_id', $cast->id)->get(),
];
return view('assign.list', compact('castName') + $relations);
}网络路由
Route::get('assign/{calls}-{name_input}', 'AssignmentsController@showAssignmentsByCastName')->name('assign.list');发布于 2017-05-01 15:26:06
可以将一个函数传递到with语句中:
'assignments' => \App\Assignment::with(['cast' => function ($query) {
$query->where('newscastId', '=', 1);
}])->get()https://stackoverflow.com/questions/43721305
复制相似问题