Hi.
The title says everything, actually. :)
I'm trying to create a basic search functionality, but there are more than one tables that I need to query. How can I include users and another additional table into my existing script and order them by name? The thing is, every table has a different name to define its names. username for users, original_name for books, etc.
public function search(Request $request)
{
if (Request::ajax())
{
$keywords = Request::input("keywords");
$arr = array();
$results = Thing::where("original_name", "LIKE", "$keywords%")->orderBy('original_name', 'ASC')->take(10)->get();
foreach($results as $result)
{
$arr[] = array('id' => $result->id, 'original_name' => $result->original_name);
}
echo json_encode($arr);
}
}
Here's my jQuery ajax code.
$('#search').on('input', function () {
var searchKeyword = $(this).val();
if (searchKeyword.length >= 3) {
$.post('/search', {keywords: searchKeyword}, function (data) {
$('ul#content').empty();
$.each(data, function () {
$('ul#content').append('<li><a href="example.php?id=' + this.id + '">' + this.original_name + '</a></li>');
});
}, "json");
}
});