Reputation: 1840
I am stuck in eloquent query! I want to show all related data only user with certain role_id. bookings:(has created_by field)
Booking Model::
public function user()
{
return $this->belongsTo('App\User','created_by');
}
public function division()
{
return $this->belongsTo('App\Division','division_id');
}
and users table has a column role_id. View:
@if(!empty($allBooking) && count($allBooking)>0)
@foreach($allBooking as $key=>$booking)
<tr>
<td>{{$key+1}}</td>
<td class="text-capitalize">
@if($booking->division && $booking->division->division_name )
{{$booking->division->division_name}}
@else
N/A
@endif
</td>
How can i get all booking data with role_id 1 ? I want to use pure eloquent to get division name too from another one to many realtion.
===============================edit
Budget Model::
public function user()
{
return $this->belongsTo('App\User','created_by');
}
public function subHead()
{
return $this->belongsTo('App\SubHead','sub_head_id');
}
SubHead Model ::
public function head()
{
return $this->belongsTo('App\Head','head_id');
}
Work Model::
public function subHead()
{
return $this->belongsTo('App\SubHead','sub_head_id');
}
public function bookings()
{
return $this->hasMany('App\Booking','work_id');
}
Upvotes: 3
Views: 3480
Reputation: 163768
If role_id
is in the users
table, use whereHas()
:
Booking::whereHas('user', function($q) use($roleId) {
$q->where('role_id', $roleId);
})->get();
Upvotes: 3