Skip to content

How do i filter datatable queries from my controller, and how do i return a collection or array to my view from datatable controller #2994

@toyintheisaac

Description

@toyintheisaac

How do i filter datatable queries from my controller, and how do i return a collection or array to my view from datatable controller

I have 2 issues.
No 1: I am trying to filter my datatable queries
eg.
When a class group is selected only record to that class show be displayed, if no class group is selected, all the class list should be displayed.

No 2: I want to show all teachers in the database to the view in yajra table.

Here is my sample code

  • My controller
    my controller
    public function getSubject(SubjectDataTable $dataTable){
        $allTeachers = (new TeacherService())->getSubjectTeacher();
        $classID = 10;
        return $dataTable->render('staffAuth.classes.subjects', compact(['allTeachers','classID']));
    }
  • My Datatable Controller
    subject datatable
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('class', 'components.subject-class')
            ->addColumn('subject_teacher', 'components.subject-teachers')  i want to be able to access  $allTeachers here in the view
            ->addColumn('action', 'components.subject-edit')
            ->escapeColumns([])
            ->setRowId('id');
    }
    public function query(Subject $model): QueryBuilder
    {
        if(!empty($this->classID)){  
            return $model->newQuery()->where('status', 'active')->where('class_id', $this->classID);
        }
        return $model->newQuery()->where('status', 'active'); 
    }

subject datatable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions