Relation Search #1124
Answered
by
luanfreitasdev
joshualedda
asked this question in
Q&A
-
Hello, I would like to ask for some help regarding my project. I want to search for the full name using the main search bar, but it's only searching within one column of the table. I've tried several methods, but it just won't work. Would you mind helping me? public function datasource(): Builder
{
return Anecdotal::query()
->join('students', 'anecdotal.student_id', '=', 'students.id')
->join('offenses', 'anecdotal.offense_id', '=', 'offenses.id')
->select(
'anecdotal.*',
'anecdotal.created_at',
'students.created_at as created',
'offenses.created_at as created_offense',
'offenses.offenses'
);
}
public function relationSearch(): array
{
return [
'students' => ['students.first_name', 'students.last_name'],
'offenses' => ['offenses'],
];
}
public function addColumns(): PowerGridColumns
{
return PowerGrid::columns()
->addColumn('first_name', function (Anecdotal $model) {
return $model->students->first_name . ' ' . $model->students->last_name;
})
// ->addColumn('last_name', function (Anecdotal $model) {
// return $model->students->last_name;
// })
->addColumn('offenses')
->addColumn('gravity', fn(Anecdotal $model) => ($model->gravityText))
->addColumn('anecdotal.created_at')
->addColumn('created_at_formatted', function (Anecdotal $model) {
return Carbon::parse($model->created_at)->format('F j, Y');
})
->addColumn('case_status', fn(Anecdotal $model) => $model->getStatusTextAttribute() ?? 'No Data');
}`
//Here the model for students
` protected $table = 'students';
protected $fillable = [
'classroom_id',
'first_name',
'middle_name',
'lrn',
'status'
];
`
//And the main modell
` protected $table = 'anecdotal';
protected $fillable = [
'student_id',
'offense_id',
'gravity',
'short_description',
'observation',
'desired',
'outcome',
'letter',
'case_status',
'status'
];
public function students()
{
return $this->belongsTo(Students::class, 'student_id');
}` |
Beta Was this translation helpful? Give feedback.
Answered by
luanfreitasdev
Sep 6, 2023
Replies: 1 comment 2 replies
-
Upppp |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Akireiss , try adding searchableRaw, like example: