Laravel 5.4 采摘,在哪里
Laravel 5.4 pluck, where
我正在研究无线电新闻广播的分配系统。试图 return 查看特定新闻广播的所有作业,但我受阻了。
表格
用户 table
id | name
---|-------
1 | Admin
2 | Susan
3 | Ed
4 | Jen
新闻广播table
id | forStation_id | name
---|---------------|-----
1 | 1 | AM
2 | 1 | PM
3 | 2 | Sports
站table
id | calls
---| -----
1 | JNDV
2 | YXWQ
作业table
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();
}
(部分)新闻广播模型
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);
}
如我所料,此代码 return 是 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 日 /assignment/list/JNDV-AM 的预期输出
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-AM (newscastId = 1) 的作业?
高级问题
您如何建议将 relations
更改为 return 仅 JNDV-AM 当前和未来的作业,首先是今天的作业,尽可能少的查询?
您可以将一个函数传递到您的 with
语句中:
'assignments' => \App\Assignment::with(['cast' => function ($query) {
$query->where('newscastId', '=', 1);
}])->get()
这是我想出的工作代码:
(部分)分配控制器
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');
我正在研究无线电新闻广播的分配系统。试图 return 查看特定新闻广播的所有作业,但我受阻了。
表格
用户 table
id | name
---|-------
1 | Admin
2 | Susan
3 | Ed
4 | Jen
新闻广播table
id | forStation_id | name
---|---------------|-----
1 | 1 | AM
2 | 1 | PM
3 | 2 | Sports
站table
id | calls
---| -----
1 | JNDV
2 | YXWQ
作业table
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();
}
(部分)新闻广播模型
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);
}
如我所料,此代码 return 是 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 日 /assignment/list/JNDV-AM 的预期输出
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-AM (newscastId = 1) 的作业?
高级问题
您如何建议将 relations
更改为 return 仅 JNDV-AM 当前和未来的作业,首先是今天的作业,尽可能少的查询?
您可以将一个函数传递到您的 with
语句中:
'assignments' => \App\Assignment::with(['cast' => function ($query) {
$query->where('newscastId', '=', 1);
}])->get()
这是我想出的工作代码:
(部分)分配控制器
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');