I have been Working on search functionality in Laravel first of all this is my ajax request that I am sending to my controller with search input
$.ajax({
url: '{{ route("invoices.search") }}',
type: 'GET',
data: {
search: searchValue
},
success: function (response) {
let tableBody = '';
$.each(response, function (index, estimate) {
let fullLink = encodeURI(estimate.invoice_link);
let truncatedLink = estimate.invoice_link.length > 20 ? estimate.invoice_link.substring(0, 20) + '...' : estimate.invoice_link;
let invoice =estimate;
tableBody += `
<tr>
<td class="text-nowrap">${estimate.id}</td>
<td class="text-nowrap">${estimate.customer ? estimate.customer.contact_name : ''}</td>
<td class="text-nowrap">${estimate.invoice_number}</td>
<td class="text-nowrap">${estimate.reference_number}</td>
<td class="text-nowrap">${estimate.gst_no}</td>
<td class="text-nowrap">${estimate.due_date}</td>
<td class="text-nowrap">${new Date(estimate.created_at).toLocaleDateString('en-US', {day: '2-digit', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit', hour12: true})}</td>
<td class="text-nowrap">${estimate.invoice_status}</td>
<td class="text-nowrap">${estimate.user.name}</td>
<td class="text-nowrap">
<a href="${fullLink}" target="_blank">${truncatedLink}</a>
</td>
<td class="text-nowrap">
${estimate.actions_html}
</td>
</tr>`;
});
$('#estimate-Table > tbody').html(tableBody);
// Reinitialize dropdowns or manually trigger them if necessary
$('[data-kt-menu-trigger="click"]').on('click', function() {
var $this = $(this);
var $menu = $this.next('.menu-sub-dropdown');
if ($menu.length) {
$menu.toggleClass('show');
}
});
}
});
Next This is My controller Logic
public function search(Request $request)
{
$role=auth()->user()->roles[0]->name;
$user_id = auth()->user()->id;
$query = Invoice::query();
$query->with([
'invoicePaymentGateways',
'invoiceLineItems',
'invoiceCustomFields',
'customer',
'user'
]);
if ($role == 'sales-person') {
$query->where('created_by', $user_id);
}
if ($request->has('search') && $request->search != '') {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('invoice_number', 'ilike', "%{$search}%") // ilike for case-insensitive LIKE in PostgreSQL
->orWhere('gst_no', 'ilike', "%{$search}%")
->orWhere('invoice_status', 'ilike', "%{$search}%")
->orWhere('reference_number', 'ilike', "%{$search}%")
->orWhereHas('customer', function ($q) use ($search) {
$q->where('contact_name', 'ilike', "%{$search}%");
})
->orWhereHas('user', function ($q) use ($search) {
$q->where('name', 'ilike', "%{$search}%");
});
});
}
$estimate_data = $query->get();
$estimate_data->transform(function ($estimate) {
$estimate->actions_html = view('pages.apps.invoice-management.invoices.columns._actions', [
'invoice' => $estimate,
'invoice_status' => $estimate->invoice_status
])->render();
return $estimate;
});
return response()->json($estimate_data);
}
here in this search I am using two inverse relations there were defined in my model they are :
public function customer()
{
return $this->belongsTo(Client::class,'customer_id','contact_id');
}
public function user()
{
return $this->belongsTo(User::class,'created_by');
}
although my search functionality works completely fine when the data-type of my contact_id field in “bigint” but due to some reasons I have the data-type of that field as “varchar(191)” where as the customer_id field of my invoice-table is “bigint” ,SO it is obvious that I am getting a match type error
So is there anything I can do so that I can perform the search operations with those different data-types
I am using PostgreSQL if that matters
I have tried to use explicit type-casting but may be I was doing it wrong or something else but that won’t work in my case, All I am looking for now is a way I can perform my search operation without changing the data-type in the database and in such a way that it won’t be affecting my others functionalities