app/Http/Controllers/Tutor/StudentsController.php
/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
Display a listing of the resource.
@return \Illuminate\Http\Response
Display a listing of the resource . @return \ Illuminate \ Http \ Response
public function index(Request $request)
    {
        $myuser = Auth::User();
        $validator = Validator::make($request->all(), [
            's_name'        => ['nullable', 'string'],
            'status'        => ['nullable', 'string'],
            'assigned_at' => ['nullable', 'date']
        ]);
        if ($validator->fails())
        {
            session() -> flash('error', $validator->message()->first());
            return back();
        }
        $request_data = $request->all();
        $assignments = $myuser->tutor_assignments();
        $q = "1=1 ";
        if (isset($request_data['status'])) {
            $q .= " and status_by_tutor like '%" . $request_data['status'] . "%'";
        } else $request_data['status'] = "";
        if (isset($request_data['assigned_at'])) {
            $q .= " and assigned_at like '%" . $request_data['assigned_at'] . "%'";
        } else $request_data['assigned_at'] = "";
        $assignments = $assignments->whereRaw($q);
        if (isset($request_data['s_name'])) {
            $assignments = $assignments->whereHas('students', function($student) use ($request_data){
                return $student->where('fname', 'like' , "%" . $request_data['s_name'] . "%")
                ->orwhere('lname', 'like', "%" . $request_data['s_name'] . "%");
            });  
        } else $request_data['s_name'] = "";
        $assignments = $assignments->get();
        $subjects_array = Array();
        foreach ($assignments as $assignment) 
        {
            $subjects_array[$assignment->id] = "";
            $subjects = $assignment->subjects()->get();
            foreach ($subjects as $subject) {
                $subjects_array[$assignment->id] .= $subject->name . ',';
            }
            $subjects_array[$assignment->id] = rtrim($subjects_array[$assignment->id], ',');
        }
        $data = [
            'subjects'      => $subjects_array,
            'assignments'   => $assignments,
            'old'           => $request_data,
        ];
        return view('tutor.students.index') -> with('data', $data);
    }
public function index ( Request $ request ) { $ myuser = Auth :: User ( ) ; $ validator = Validator :: make ( $ request -> all ( ) , [ 's_name' => [ 'nullable' , 'string' ] , 'status' => [ 'nullable' , 'string' ] , 'assigned_at' => [ 'nullable' , 'date' ] ] ) ; if ( $ validator -> fails ( ) ) { session ( ) -> flash ( 'error' , $ validator -> message ( ) -> first ( ) ) ; return back ( ) ; } $ request_data = $ request -> all ( ) ; $ assignments = $ myuser -> tutor_assignments ( ) ; $ q = " 1=1  " ; if ( isset ( $ request_data [ 'status' ] ) ) { $ q .= "  and status_by_tutor like '% " . $ request_data [ 'status' ] . " %' " ; } else $ request_data [ 'status' ] = " " ; if ( isset ( $ request_data [ 'assigned_at' ] ) ) { $ q .= "  and assigned_at like '% " . $ request_data [ 'assigned_at' ] . " %' " ; } else $ request_data [ 'assigned_at' ] = " " ; $ assignments = $ assignments -> whereRaw ( $ q ) ; if ( isset ( $ request_data [ 's_name' ] ) ) { $ assignments = $ assignments -> whereHas ( 'students' , function ( $ student ) use ( $ request_data ) { return $ student -> where ( 'fname' , 'like' , " % " . $ request_data [ 's_name' ] . " % " ) -> orwhere ( 'lname' , 'like' , " % " . $ request_data [ 's_name' ] . " % " ) ; } ) ; } else $ request_data [ 's_name' ] = " " ; $ assignments = $ assignments -> get ( ) ; $ subjects_array = Array ( ) ; foreach ( $ assignments as $ assignment ) { $ subjects_array [ $ assignment -> id ] = " " ; $ subjects = $ assignment -> subjects ( ) -> get ( ) ; foreach ( $ subjects as $ subject ) { $ subjects_array [ $ assignment -> id ] .= $ subject -> name . ',' ; } $ subjects_array [ $ assignment -> id ] = rtrim ( $ subjects_array [ $ assignment -> id ] , ',' ) ; } $ data = [ 'subjects' => $ subjects_array , 'assignments' => $ assignments , 'old' => $ request_data , ] ; return view ( 'tutor.students.index' ) -> with ( 'data' , $ data ) ; }
Display a listing of the resource.
Display a listing of the resource .