仅在有可用插槽时选择事件

时间:2018-11-13 07:07:27

标签: php mysql laravel

我正在尝试仅选择具有可用插槽的事件。最大插槽由我的事件表上的一列定义,而占用的插槽从邀请表中计数。问题是,邀请计数应用于所有事件,而不是每个事件ID,并且每个事件显示的成员就是所有已接受任何内容的所有受邀用户。

编辑:如何通过视图上的调用函数传递变量?我认为,如果可以将eventID传递到countMembers函数中,则可以解决计数问题。

Edit2:已解决。只需将查询移到其自己的函数中,然后将id作为参数传递即可。

选择事件

      $eventCompetition=DB::table('events')
            ->leftjoin('users', 'users.id', '=' , 'events.user_id')
            ->leftjoin('invitations','invitations.event_id', '=', 'events.eventID')
            ->where('events.eventType','=', 'Competition')
            ->where('events.eventStatus','=', 'UPCOMING')
            ->where('events.eventIsArchived','=', '0')
            ->where('events.user_id','!=',Auth::user()->id)
            ->where('events.eventSlots','>', 
                (DB::table('invitations')
                ->leftjoin('users', 'users.id', '=', 'invitations.sender_id')
                ->leftjoin('events', 'events.eventID', '=', 'invitations.event_id')
                ->whereColumn('invitations.event_id','events.eventID')
                ->where('invitations.status','=','ACCEPTED')
                ->count())
            )
            ->orderBy('events.eventDateStart','DESC')
            ->paginate(10);

查看会员

        $members=DB::table('invitations')
            ->leftjoin('users', 'users.id', '=', 'invitations.sender_id')
            ->leftjoin('events', 'events.eventID', '=', 'invitations.event_id')
            ->whereColumn('invitations.event_id','events.eventID')
            ->where('invitations.status','=','ACCEPTED')
            ->orderBy('users.fName')
            ->paginate(10);

计数成员

    public static function countMembers()
    {
        //
        $members=DB::table('invitations')
            ->leftjoin('users', 'users.id', '=', 'invitations.sender_id')
            ->leftjoin('events', 'events.eventID', '=', 'invitations.event_id')
            ->whereColumn('invitations.event_id','events.eventID')
            ->where('invitations.status','=','ACCEPTED')
            ->count();

        return $members;
    }

查看

@foreach($eventCompetition as $event)
<form action = "/joinEvent/{{$event->eventID}}" method = "post">
<div id="viewEvent{{$event->eventID}}" class="modal fade">
<div class="modal-dialog modal-login">
    <div class="modal-content">
        <div class="modal-header text-white"style="background-color: #018BB5">        
            <h3 class="modal-title" style="color:black;"><strong>Event Info</strong></h3>
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><h1>&times;</h1></button>
        </div>
        <div class="modal-body form-row col-md-12">
            <table class="table table-striped" style="color:black;">
                <thead>
                    <input type="hidden" id="receive" name="receive" value="{{$event->user_id}}">
                    @csrf
                    <tr>
                        <th scope="row">Event Name:</th>
                        <td>{{$event->eventName}}</td>              
                    </tr>
                    <tr>
                        <th scope="row">Event Type:</th>
                        <td>{{$event->eventType}}</td>              
                    </tr>
                    <tr>
                        <th scope="row">Max Slots:</th>
                        <td>{{$event->eventSlots}}</td>              
                    </tr>
                    <tr>
                        <th scope="row">Address:</th>
                        <td>{{$event->eventAddress}}</td>              
                    </tr>
                    <tr>
                        <th scope="row">Sport:</th>
                        <td>{{$event->eventSport}}</td>              
                    </tr>
                    <tr>
                        <th scope="row">Date:</th>
                        <td><?php echo Carbon::parse($event->eventDateStart)->format('d F Y h:i:s A'); ?> <br>to <br><?php echo Carbon::parse($event->eventDateEnd)->format('d F Y h:i:s A'); ?></td>              
                    </tr>
                    <tr>
                        <th scope="row">Details:</th>
                        <td>{{$event->eventDetails}}</td>              
                    </tr>
                    <tr>
                        <th scope="row">Members (<?php echo EventController::countMembers(); ?>/{{$event->eventSlots}}):</th>
                        <td>
                            @foreach($members as $member)
                                <?php
                                    echo "<br>$member->fName $member->mName $member->lName";
                                ?>
                            @endforeach
                        </td>              
                    </tr>
                </thead>
            </table>
            <?php
                if($event->sender_id != Auth::user()->id){
                    echo "<button type='button' class='btn btn-info btn-lg text-white' data-toggle='modal'  data-target='#exampleModal$event->eventID'><strong>Send Join Request</strong></button>";
                    }
                ?>
            </div>                
        </div>
    </div>    
</div>  
</form>
@endforeach

1 个答案:

答案 0 :(得分:2)

根据建议,这是我的解决方法

将嵌套查询替换为静态函数

选择事件

$eventCompetition=DB::table('events')
            ->leftjoin('users', 'users.id', '=' , 'events.user_id')
            ->leftjoin('invitations','invitations.event_id', '=', 'events.eventID')
            ->where('events.eventType','=', 'Competition')
            ->where('events.eventStatus','=', 'UPCOMING')
            ->where('events.eventIsArchived','=', '0')
            ->where('events.user_id','!=',Auth::user()->id)
            ->where('events.eventSlots','>', $this->countMembers('event.eventID'))
            ->orderBy('events.eventDateStart','DESC')
            ->distinct()
            ->paginate(10);

计数并查看成员

    public static function countMembers($id)
    {
        //
        $members=DB::table('invitations')
            ->leftjoin('users', 'users.id', '=', 'invitations.sender_id')
            ->leftjoin('events', 'events.eventID', '=', 'invitations.event_id')
            ->where('invitations.event_id','=', $id)
            ->where('invitations.status','=','ACCEPTED')
            ->count();

        return $members;
    }

    public static function getMembers($id)
    {
        //
        $members=DB::table('invitations')
            ->leftjoin('users', 'users.id', '=', 'invitations.sender_id')
            ->leftjoin('events', 'events.eventID', '=', 'invitations.event_id')
            ->where('invitations.event_id','=',$id)
            ->where('invitations.status','=','ACCEPTED')
            ->orderBy('users.fName')
            ->get();

        return $members;
    }

查看

@foreach($eventTraining as $event)
<form action = "/joinEvent/{{$event->eventID}}" method = "post">
<div id="viewEvent{{$event->eventID}}" class="modal fade">
            <div class="modal-dialog modal-login">
                    <div class="modal-content">
                            <div class="modal-header text-white"style="background-color: #018BB5">        
                            <h3 class="modal-title" style="color:black;"><strong>Event Info</strong></h3>
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><h1>&times;</h1></button>
                            </div>
                            <div class="modal-body form-row col-md-12">
                                    <table class="table table-striped" style="color:black;">
                                            <thead>
                                                <input type="hidden" id="receive" name="receive" value="{{$event->user_id}}">
                                                @csrf
                                                    <tr>
                                                            <th scope="row">Event Name:</th>
                                                            <td>{{$event->eventName}}</td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Event Type:</th>
                                                            <td>{{$event->eventType}}</td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Max Slots:</th>
                                                            <td>{{$event->eventSlots}}</td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Address:</th>
                                                            <td>{{$event->eventAddress}}</td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Sport:</th>
                                                            <td>{{$event->eventSport}}</td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Date:</th>
                                                            <td><?php echo Carbon::parse($event->eventDateStart)->format('d F Y h:i:s A'); ?> <br>to <br><?php echo Carbon::parse($event->eventDateEnd)->format('d F Y h:i:s A'); ?></td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Details:</th>
                                                            <td>{{$event->eventDetails}}</td>              
                                                    </tr>
                                                    <tr>
                                                            <th scope="row">Members (<?php echo EventController::countMembers($event->eventID); ?>/{{$event->eventSlots}}):</th>
                                                            <td>
                                                                <?php
                                                                    $members = EventController::getMembers($event->eventID);
                                                                    foreach($members as $member):
                                                                        echo $member->fName." ".$member->mName." ".$member->lName;
                                                                    endforeach
                                                                ?>
                                                            </td>              
                                                    </tr>
                                            </thead>
                                    </table>
                                    <?php
                                        if($event->sender_id != Auth::user()->id){
                                            echo "<button type='button' class='btn btn-info btn-lg text-white' data-toggle='modal'  data-target='#exampleModal$event->eventID'><strong>Send Join Request</strong></button>";
                                        }
                                    ?>
                            </div>                
                    </div>
            </div>    
        </div>  
    </form>
 @endforeach