I used Yajra/Laravel-Datatables to manage the records, and I added a searching feature for this table. I'm having trouble with the three-level relationship record search.
tables
- Lead table
- Agent table
- User table
$data = LeadInquiry::select(
'lead_inquiries.*',
'agent_leads.agent_id',
'sender_detail.first_name as sender_first_name',
'sender_detail.last_name as sender_last_name',
'sender_detail.email as sender_email',
'receiver_detail.first_name as
receiver_first_name',
'receiver_detail.last_name as receiver_last_name',
'receiver_detail.email as receiver_email'
)
->join('agent_leads', 'agent_leads.lead_id', 'lead_inquiries.map_id')
->join(
'users as sender_detail',
'lead_inquiries.sender_id',
'sender_detail.id'
)
->join(
'users as receiver_detail',
'lead_inquiries.receiver_id',
'receiver_detail.id'
)
->where('agent_leads.agent_id', Auth::user()->id)
->latest();
filter(function ($query) use ($search) {
if ($search) {
$query->where(function ($query) use ($search) {
$query->Where('user', function ($query) use ($search) {
$query->orwhere('first_name', 'like', '%' . $search . '%');
$query->orwhere('last_name', 'like', '%' . $search . '%');
$query->orwhere('email', 'like', '%' . $search . '%');
});
});
}
});
CodePudding user response:
Try replacing -
filter(function ($query) use ($search) {
if ($search) {
$query->where(function ($query) use ($search) {
$query->Where('user', function ($query) use ($search) {
$query->orwhere('first_name', 'like', '%' . $search . '%');
$query->orwhere('last_name', 'like', '%' . $search . '%');
$query->orwhere('email', 'like', '%' . $search . '%');
});
});
}
});
with
if ($search) {
$data->where(function ($query) use ($search) {
$query->where('sender_detail.first_name', 'like', '%' . $search . '%');
$query->orwhere('sender_detail.last_name', 'like', '%' . $search . '%');
$query->orwhere('sender_detail.email', 'like', '%' . $search . '%');
});
}