首页 > 解决方案 > 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 周)

index.blade.php 和调试栏的图像

安排 MySQL

联赛模式


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');
    }}

edd($schedules)

标签: phplaravelbootstrap-4pagination

解决方案


如果您只想获取本周的日程安排,您可以在查询中使用 Carbon,如下所示:

$schedules = $league->schedule->where('weekIndex', Carbon::now()->week())

编辑:如果您希望它们分组,只需使用 groupBy,如下所示:

$schedules = $league->schedule->groupBy('weekIndex');

我想这就是你要找的


推荐阅读