It's a project using laravel with php as programming language(for website also) and mysql as database. on database, it's 3 tables I used for users, roles, and role_user(this is the relation table). I can manage users as Admin on webpage of my project. However what I want to ask is, how to filter search with relation they had? it's working if it only search the relation(gets everything that had relation I wanted) but failed when added search the word alike for table(gets everything that has the word I search but failed to filter it using the relation)
Using example:
Result I wanted: users with the roles of 'Teacher' and name 'X'
Result I got: users with the name 'X'(somehow the relation code filter gets ignored)
Table that get the result of searching the relation/Table gets every data that had the relation
Supposed Result/User Student Table
Supposed Result/User Teacher Table
Search Table but failed to filter it using relation
Failed Teacher search using relation
Failed Student search using relation
Because the page show up, I guess it's the controllers code that needed to be edited for making my supposed search result.
AdminControllers.php
Models used:
use App\User;
use App\Role;
Codes for tables and search:
/**
* FOR TEACHER
*
*/
// List(this part is working as inteded)
public function showTeacherList()
{
$user = Auth::user(); // Untuk Photo Profile
$teachers = User::whereHas('roles', function($q){
$q->where('name', 'Teacher');
})->get();
return view('pages.admin.user.teacher.showTeacherList', compact('user', 'teachers') );
}
/*
* This is For Show Search Teacher(Well, the page is working but the filter for relation isn't)
*
*/
public function searchTeacher(Request $request)
{
$user = Auth::user(); // Untuk Photo Profile
// menangkap data pencarian
$search = $request->table_search;
// This part is the code that supposed to filter the search that has relation to data named Teacher in name column
$search = User::whereHas('roles', function($q){
$q->where('name', 'Teacher');
})->where('name','like',"%".$search."%")
->orWhere('nip','like',"%".$search."%")
->orWhere('username','like',"%".$search."%")
->get();
// what I got is actually the normal search, so all other relation than Teacher also show up
return view('pages.admin.user.teacher.showTeacherFiltered', compact('search', 'user') );
}
/**
* FOR Student
*
*/
//Working as intended
public function showStudentList()
{
$user = Auth::user(); // Untuk Photo Profile
$students = User::whereHas('roles', function($q){
$q->where('name', 'Student');
})->get();
return view('pages.admin.user.student.showStudentList', compact('user', 'students'));
}
/*
* This is For Show Search Student
*
*/
//Page is working but not the filter using relation
public function searchStudent(Request $request)
{
$user = Auth::user(); // Untuk Photo Profile
// menangkap data pencarian
$search = $request->table_search;
// This part is the code that supposed to filter the search that has relation to data named Student in name column
$search = User::whereHas('roles', function($q){
$q->where('name', 'Student');
})->where('name','like',"%".$search."%")
->orWhere('nisn','like',"%".$search."%")
->orWhere('username','like',"%".$search."%")
->get();
// // what I got is actually the normal search, so all other relation than Student also show up
return view('pages.admin.user.student.showStudentFiltered', compact('search', 'user') );
}
Did anyone know the code to filter search using the relation? it's relation Many-To-Many.