Reputation: 5608
I have the following foreach, Accessing the relationship approved_applicants
. Where i am checking the number of array objects
.
The current condition is giving me two buttons because DB
have to two records
where i want to restrict it to one.
@foreach($candidate->approved_applicants() as $vote)
@if(count($candidate->approved_applicants) >= 2 || $vote->type == 'm')
<button type="submit" class="btn btn-success" disabled="">Approved</button>
@else
<button type="submit" class="btn btn-warning" >Approve</button>
@endif
@endforeach
Controller method :
$pendingRequests = \App\Applicant::with('approved_applicants')->where('company_name',auth()
->user()->company_name)->get();
Relationship :
public function approved_applicants(){
return $this->hasMany('App\ApprovedApplicant');
}
I was using the distinct()
method on relationship but it didn't worked.
Upvotes: 1
Views: 1005
Reputation: 21681
I think you can try this :
$pendingRequests = \App\Applicant::with('approved_applicants')->where('company_name',auth()
->user()->company_name)->groupBy('user_id')->get();
For more details please see this question.
Upvotes: 0
Reputation: 11
It will work:
$pendingRequests = \App\Applicant::with(['approved_applicants' => function($query) {
$query->groupBy('user_id');
}])->where('company_name',auth()
->user()->company_name)->get();
Upvotes: 1
Reputation: 630
Please try this solution:
$pendingRequests = \App\Applicant::with(['approved_applicants' => function($q) {
$q->groupBy('user_id');
}])->where('company_name',auth()
->user()->company_name)->get();
Upvotes: 0