我试图仅选择有可用插槽的事件。最大空位由我的事件表中的一列定义,而占用的空位则从邀请表中计算。问题是,邀请计数适用于所有事件,而不是适用于每个事件 ID,并且每个事件显示的成员只是已接受任何事件的所有受邀用户。
编辑:如何通过 View 上的被调用函数传递变量?我想如果我可以将 eventID 传递到我的 countMembers 函数中,那么就可以修复计数问题。
编辑2:已解决。只需将查询移动到它们自己的函数中并将 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);
查看成员(member)
$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>×</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
最佳答案
根据建议,这是我的解决方案
用静态函数替换嵌套查询
选择事件
$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>×</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
关于php - 仅当有空位时才选择事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53275601/