Is there a way to retrieve data from both the Employee and Company tables based on a search by Company name rather than Company Id? These tables have a one-to-many relationship. The current search functionality is working well, but any assistance on implementing this specific search feature would be greatly appreciated.
Here is the code in the EmployeeController:
public function search(){
if($search = \Request::get('q')){
$employees = Employee::where(function($query) use ($search){
$companies = Company::Where('id','company_id');
$searcompany ?????
foreach($companies AS $company){
$searcompany = $company->Company;
}
$query->Where('BadgeCode','LIKE',"%$search%")
->orWhere($searcompany,'LIKE',"%$search%")
})->orderBy('BadgeCode','desc')->paginate(20);
// return $users;
} else{
$employees = Employee::latest()->paginate(5);
}
return $employees;
}
HTML Code in Employee.vue:
<div style="margin-left:450px; margin-top:0px;margin-bottom:-10px;">
<h3 class="card-title">
<div class="card-tools">
<div class="input-group input-group-sm" style="width: 250px;">
<input
name="table_search"
class="form-control float-right"
placeholder="Search"
@keyup="searchit"
v-model="search"
type="search"
aria-label="Search"
/>
<div class="input-group-append">
<button type="submit" class="btn btn-default" @click="searchit">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</div>
</h3>
</div>
Script Code in Employee.vue:
export default {
data() {
return {
employees :{},
search: "",
},
methods: {
searchit: _.debounce(() => {
Fire.$emit("searching");
}, 300),
},
created() {
Fire.$on("searching", () => {
let query = this.search;
axios
.get("api/findemployee?q=" + query)
.then(data => {
this.employees = data.data;
})
.catch(() => {});
});
}
}
The API Route is:
Route::get('findemployee','API\EmployeeController@search');