Search code examples
phplaravellaravel-5.3

Laravel relations where the relation brings the correct users data


So I tried setting up somewhat of an inhouse sports page for my forum users where they basically can enter who they think will win the game and then after the result has come in they will be awarded points if they were correct.

While doing this I ran into an issue. So far I have the games in the database and then I created an Results relation. The problem is that when the user is viewing all the games on a said date (Several events) then the relation doesn't know which one belongs to said user. I tried for a couple of hours now but I am honestly drawing a blank. Take a look at this:

Route:

Route::get('/event/{date}/list', 'Other\OtherController@indexDateGames');

Controller:

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

Model Result :

    public function event()
{
    return $this->belongsTo(Event::class);
}

Model Event :

        public function result()
{
    return $this->hasOne(Result::class);
}

View:

<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>

Does anybody know how to make it so that the relation basically knows that "$event->result" Is actually YOUR result? I know this doesnt work, but this is just an example:

$event->result(WhereUserId($uid)->result

Solution

  • In this case the event should have many results

    public function results()
    {
        return $this->hasMany(Result::class);
    }
    

    For the query :

    $events = Event::with(['results' => function ($query) {
                        $query->where('user_id', '=', Auth::user()->id);
                    }])
                    ->where('date', $date)
                    ->get();