简体   繁体   English

Laravel - 显示特定列的结果(分页)

[英]Laravel - display results from specific column (paginate)

Morning all,大家早,

I am seeking assistance enabling me to display weekly schedules in my league.我正在寻求帮助,使我能够在我的联盟中显示每周时间表。 The schedules I would like to be shown week by week identified in MySQL by column weekIndex.我希望每周显示的时间表在 MySQL 中按列 weekIndex 标识。

As of now, I can display all the schedules on one blade.截至目前,我可以在一个刀片上显示所有时间表。 Hopefully below I have included enough detail.希望下面我已经包含了足够的细节。

SchedulesController调度控制器


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

Schedule Model附表 Model


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

Schedule index.blade.php附表 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>

I have included an image below showing the debugbar and it displaying all 333 schedules.我在下面包含了一张显示调试栏的图片,它显示了所有 333 个计划。 Each schedule belongs to a weekIndex in MySQL (0-22 weeks)每个schedule在MySQL(0-22周)中属于一个weekIndex

index.blade.php 和 debugbar 的图片

调度 MySQL

League Model联赛 Model


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($时间表)

If you want to get only schedules for current week, you can use Carbon in your query, like this:如果您只想获取本周的时间表,您可以在查询中使用 Carbon,如下所示:

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

EDIT: If you want them grouped, just use groupBy, like this:编辑:如果你想将它们分组,只需使用 groupBy,如下所示:

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

I think that's what you're looking for我想这就是你要找的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM