我已经使用AJAX创建了实时搜索,用户可以在其中搜索作业,并且可以从我的employer_profiles
表中返回数据,特别是两列company_name
和company_address
。我有一个名为job_posts
的第二张表,带有2列job_title
和job_description
。在这里,登录的雇主可以在他们想要雇用某人时为其公司创建工作。当然这里有关系,两个表都有一个user_id列。
我想在我的表中显示3列,company_name
表中的company_address
和employer_profiles
,以及job_title
表中的job_posts
。
是否有JOIN会有所帮助?如果是,我该怎么做?这是我仅从employer_profiles
表中获取的代码。
AdminJobSeekerSearchController.php:
public function action(Request $request)
{
if($request->ajax()){
$output = '';
$query = $request->get('query');
if($query != ''){
$data = DB::table('employer_profiles')
->where('company_name', 'like', '%'.$query.'%')
->orWhere('company_address', 'like', '%'.$query.'%')
->get();
}else{
$data = DB::table('employer_profiles')
->orderBy('id', 'desc')
->get();
}
$total_row = $data->count();
if($total_row > 0){
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->company_name.'</td>
<td>'.$row->company_address.'</td>
<td><a type="button" href="/admin/job-seeker/search/employer/'.$row->id.'" class="btn btn-primary">View</a></td>
</tr>
';
}
}else{
$output = '
<tr>
<td colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
return response()->json($data);
}
}
index.blade:
@extends('layouts.admin')
@section('content')
@if (session('send-profile'))
<div class="alert alert-success">
{{ session('account') }}
</div>
@endif
<div class="row">
<div class="col-md-12">
<!-- Topbar Search -->
<form class="navbar-search">
<div class="input-group">
<input type="text" class="form-control bg-lightblue border-0 small text-white border-dark" name="search" id="search" placeholder="Search for..." aria-label="Search" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-success" type="button">
<i class="fas fa-cannabis"></i>
</button>
</div>
</div>
</form><br>
</div>
<div class="col-md-12">
<table class="table table-hover table-responsive-sm">
<thead class="thead-dark">
<tr class="align-items-center"><th colspan="4" style="text-align: center;"><h5><strong>Total Results: <span id="total_records"></span></strong></h5></th></tr>
<tr>
<th scope="col">Company Name</th>
<th scope="col">Immediate Contact</th>
<th scope="col">Address</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
{{-- Show Pagination --}}
<div class="row">
<div class="col-md-6 offset-md-3 d-flex justify-content-center">
{{$employerProfiles->render()}}
</div>
</div>
@stop
@push('scripts')
<script>
$(document).ready(function() {
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('admin.job.seeker.search.action') }}",
method: 'GET',
data: {query:query},
dataType:'json',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query);
})
});
</script>
@endpush
web.php
Route::get('/admin/job-seeker/search/action', 'AdminJobSeekerSearchController@action')->name('admin.job.seeker.search.action')->middleware('verified');
希望这使您有足够的了解如何做。
假设这些表:
CREATE TABLE `employer_profiles` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`company_name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`company_address` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;
和
CREATE TABLE `job_posts` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`employer_profile_id` int(10) unsigned NOT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
这里有一些数据
INSERT INTO `employer_profiles` (`id`, `company_name`, `company_address`, `created_at`, `updated_at`)
VALUES
(1, 'Bolay', '123 main street', '2020-03-29 07:18:30', '2020-03-29 07:18:30'),
(2, 'Barones Pizza', '345 Sample road', '2020-03-29 07:18:56', '2020-03-29 07:18:56'),
(3, 'Sigma Electronics', '34 Yamato Road', '2020-03-29 07:23:21', '2020-03-29 07:23:21');
工作:
INSERT INTO `job_posts` (`id`, `employer_profile_id`, `description`, `created_at`, `updated_at`)
VALUES
(1, 1, 'Cashier', '2020-03-29 07:20:18', '2020-03-29 07:20:23'),
(2, 1, 'Food prep', '2020-03-29 07:20:35', '2020-03-29 07:20:35'),
(3, 2, 'Chef', '2020-03-29 07:23:41', '2020-03-29 07:23:41'),
(4, 3, 'QA engineer', '2020-03-29 07:24:00', '2020-03-29 07:24:00');
使用数据库外观的典型代码:
$data = DB::table('employer_profiles')
->join('job_posts', 'job_posts.employer_profile_id', '=', 'employer_profiles.id')
->get();
foreach($data as $row){
echo json_encode($row) . "\n";
}
使用雄辩
EmployerProfile.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class EmployerProfile extends Model
{
public function jobposts()
{
return $this->hasMany('App\JobPost');
}
}
JobPost.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class JobPost extends Model
{
//
}
然后:
use App\JobPost;
use App\EmployerProfile;
$all = EmployerProfile::all();
foreach($all as $emp) {
$this->line($emp->company_name);
foreach($emp->jobposts as $post) {
$this->line(' ' . $post->description);
}
}