php - Laravel - 显示特定列的结果(分页)
问题描述
大家早,
我正在寻求帮助,使我能够在我的联赛中显示每周的日程安排。我希望在 MySQL 中按 weekIndex 列逐周显示的时间表。
到目前为止,我可以在一个刀片上显示所有计划。希望下面我已经包含了足够的细节。
调度控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Schedule;
use App\Models\Team;
use App\Models\League;
use App\Models\Player;
use App\Models\Standing;
use App\Models\Stat;
class SchedulesController extends Controller
{
function index(League $league, Team $teams, Schedule $schedules){
$schedules = $league->schedule;
$teams = $league->team;
return view('schedules.index', compact('league','schedules', 'teams'));
}
function show($league, $schedule, Team $team){
$schedule = Schedule::where('leagueId', $league)->where('scheduleId', $schedule)->get();
$league = League::find($league);
$team = $league->team;
return view('schedules.show')->withSchedule($schedule)->withLeague($league)->withTeam($team);
}}
时间表模型
namespace app\Models;
use Illuminate\Database\Eloquent\Model;
class Schedule extends Model
{
protected $table = 'schedule';
public $primaryKey = 'scheduleId';
public function profile()
{
return $this->hasMany('App\Models\Profile');
}
public function league()
{
return $this->belongsTo('App\Models\League', 'leagueId');
}
public function player()
{
return $this->hasMany('App\Models\Player', 'leagueId');
}
public function team()
{
return $this->hasMany('App\Models\Team', 'leagueId');
}
public function stat()
{
return $this->hasMany('App\Models\Stat', 'statId');
}
}
调度 index.blade.php
@foreach($schedules as $schedule)
<tr>
<td class="team-schedule__away"><a href=""><img src="/images/teams/64/{{$schedule->awayTeamId}}.png"></a></td>
<td class="team-schedule__awayscore">{{$schedule->awayScore}}</td>
<td class="team-schedule__at"><img src="/images/at.png"alt="at" width="50px"></td>
<td class="team-schedule__homescore">{{$schedule->homeScore}}</td>
<td class="team-schedule__home"><a href=""><img src="/images/teams/64/{{$schedule->homeTeamId}}.png"></a></td>
<td class="team-schedule__tickets"><a href="./result/{{$schedule->scheduleId}}" class="btn btn-xs btn-default-alt btn-block ">Preview</a></td>
</tr>
@endforeach
@else
<p>
<div class="col-md-12">
<div class="alert alert-danger"><button type="button" class="close" aria-label="Close"><span aria-hidden="true"></span></button> <strong>Schedules Not Found!</strong> Please try to sync your league following the included instructions HERE</div>
</div>
@endif
</tbody>
我在下面包含了一个显示调试栏的图像,它显示了所有 333 个时间表。MySQL 中每个 schedule 都属于一个 weekIndex(0-22 周)
联赛模式
namespace app\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class League extends Model
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'leagues';
public $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = [
'id',
];
/**
* The attributes that are hidden.
*
* @var array
*/
protected $hidden = [];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];
/**
* Fillable fields for a Profile.
*
* @var array
*/
protected $fillable = [
'name',
'shortName',
'exportId',
'platform',
'taggable_id',
'taggable_type',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'name' => 'string',
'shortName' => 'string',
'exportId' => 'string',
'platform' => 'string',
'taggable_id' => 'integer',
'taggable_type' => 'string',
];
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return array
*/
public static function rules($id = 0, $merge = [])
{
return array_merge(
[
'name' => 'required|min:3|max:50|unique:leagues,name'.($id ? ",$id" : ''),
'shortName' => 'required|min:3|max:10|unique:leagues,shortName'.($id ? ",$id" : ''),
'exportId' => 'max:5',
'platform' => 'required',
],
$merge
);
}
/**
* Get the profiles for the league.
*/
public function profile()
{
return $this->hasMany('App\Models\Profile');
}
public function team()
{
return $this->hasMany('App\Models\Team', 'leagueId');
}
public function player()
{
return $this->hasMany('App\Models\Player', 'leagueId');
}
public function schedule()
{
return $this->hasMany('App\Models\Schedule', 'leagueId');
}
public function stat()
{
return $this->hasMany('App\Models\Stat', 'leagueId');
}}
解决方案
如果您只想获取本周的日程安排,您可以在查询中使用 Carbon,如下所示:
$schedules = $league->schedule->where('weekIndex', Carbon::now()->week())
编辑:如果您希望它们分组,只需使用 groupBy,如下所示:
$schedules = $league->schedule->groupBy('weekIndex');
我想这就是你要找的
推荐阅读
- postgresql - 当 wal_level 设置为最小时,postgres 11.4 无法启动
- java - 在 Intellij 中运行 Java 应用程序时无法读取 AWS SDK 的环境变量
- python - 在 C++ 中嵌入 python 包
- jmeter - JMeter:JMeter 是否为 5.2.1 的 webserice(Http) 调用连接池
- ios - UITextField 文本不刷新
- sql - 如何计算 SQL 字段中值较低的记录?
- c - 在 Linux 中构建 PLX SDK 驱动程序会产生“没有这样的文件或目录”
- python - 将 CSV 文件的二维数组值读取为二维数组而不是字符串
- python - 将 SQL 转换为 Django ORM 查询(“Lead”函数内“Window”内的“IF”语句)
- r - 循环遍历 R 中的列表