所以我尝试为我的论坛用户设置一些内部体育页面,他们基本上可以输入他们认为会赢得游戏的人,然后在结果出来之后,如果他们是正确的,他们将获得积分。
在这样做时我遇到了一个问题。到目前为止,我在数据库中有游戏,然后我创建了一个结果关系。 问题是当用户在所述日期(几个事件)上观看所有游戏时,该关系不知道哪个游戏属于所述用户。我现在尝试了几个小时,但我老实说是空白。看看这个:
路线:
Route::get('/event/{date}/list', 'Other\OtherController@indexDateGames');
控制器:
public function indexDateGames($date)
{
$dateToday = (new \DateTime())->format('Y-m-d H:i:s');
$events = Event::where('date', $date)->get();
$uid = Auth::user()->id;
$vars['uid'] = $uid;
$vars['date'] = $date;
$vars['dateToday'] = $dateToday;
$vars['events'] = $events;
if ($date >= $dateToday) {
$vars['url'] = 'eventsUpcoming';
} else {
$vars['url'] = 'eventsPassed';
}
return view('other.event_on_date', $vars);
}
模型结果:
public function event()
{
return $this->belongsTo(Event::class);
}
模特赛事:
public function result()
{
return $this->hasOne(Result::class);
}
查看:
<div class="row">
<div class="col-lg-6">
<div id="spinner" style="margin-top:200px;margin-left:47%;">
<i class="fa fa-spinner fa-spin fa-5x fa-fw"></i>
</div>
<div id="data" style="display:none;margin:10px;">
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Tid</th>
<th>Resultat</th>
<th>Ditt val</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Tid</th>
<th>Resultat</th>
<th>Ditt val</th>
</tr>
</tfoot>
<tbody>
@foreach($events as $event)
@if ('2017-09-16 16:00:00' < $event->start)
<tr>
<td>{{$event->start->format('H:i')}}</td>
<td>
@if (isset($event->result->result))
@if ($event->result->result == 1)
<a href="#" class="btn btn-success btn-xs" role="button" aria-pressed="true">{{$event->home}}</a>
<a href="{{ URL::to('/event/' . $event->id . '/changeresult/2/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">X</a>
<a href="{{ URL::to('/event/' . $event->id . '/changeresult/3/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">{{$event->away}}</a>
@elseif ($event->result->result == 2)
<a href="{{ URL::to('/event/' . $event->id . '/changeresult/1/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">{{$event->home}}</a>
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">X</a>
<a href="{{ URL::to('/event/' . $event->id . '/changeresult/3/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">{{$event->away}}</a>
@elseif ($event->result->result == 3)
<a href="{{ URL::to('/event/' . $event->id . '/changeresult/1/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">{{$event->home}}</a>
<a href="{{ URL::to('/event/' . $event->id . '/changeresult/2/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">X</a>
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">{{$event->away}}</a>
@endif
@else
<a href="{{ URL::to('/event/' . $event->id . '/result/1/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">{{$event->home}}</a>
<a href="{{ URL::to('/event/' . $event->id . '/result/2/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">X</a>
<a href="{{ URL::to('/event/' . $event->id . '/result/3/' . $uid) }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">{{$event->away}}</a>
@endif
</td>
@elseif (isset($event->final_result))
<tr>
<td>{{$event->start->format('H:i')}}</td>
<td>
@if ($event->final_result == 1)
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">{{$event->home}} vann matchen över {{$event->away}}</a>
@elseif ($event->final_result == 2)
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">Matchen slutade lika mellan {{$event->home}} och {{$event->away}}</a>
@elseif ($event->final_result == 3)
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">{{$event->away}} vann matchen över {{$event->home}}</a>
@endif
</td>
@else
<tr>
<td>{{$event->start->format('H:i')}}</td>
<td>
<a href="{{ URL::to('#') }}" class="btn btn-danger btn-xs" role="button" aria-pressed="true">Matchen låst - Resultat ej fastställt</a>
</td>
@endif
@if (isset($event->result->result))
<td>
@if ($event->result->result == 1)
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">{{$event->home}}</a>
@elseif ($event->result->result == 2)
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">Lika</a>
@elseif ($event->result->result == 3)
<a href="{{ URL::to('#') }}" class="btn btn-success btn-xs" role="button" aria-pressed="true">{{$event->away}}</a>
@endif
</td>
</tr>
@else
<td>
<a href="{{ URL::to('#' . $event->id . '/result/1') }}" class="btn btn-default btn-xs" role="button" aria-pressed="true">Inget val gjort</a>
</td>
</tr>
@endif
@endforeach
</tbody>
</table>
</div>
</div>
</div>
有人知道如何制作它以便关系基本上知道这一点 “$ event-&gt;结果”实际上是你的结果?我知道这不起作用,但这只是一个例子:
$event->result(WhereUserId($uid)->result
答案 0 :(得分:1)
在这种情况下,事件应该有很多结果
public function results()
{
return $this->hasMany(Result::class);
}
对于查询:
$events = Event::with(['results' => function ($query) {
$query->where('user_id', '=', Auth::user()->id);
}])
->where('date', $date)
->get();