app/Http/Controllers/Admin/AssignmentsController.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)
    {
        $s_data = $this->validate($request, [
            's_name' => 'nullable|string',
            't_name' => 'nullable|string',
            'a_date' => 'nullable|date',
        ]);
        $field = isset($_GET['field']) ? trim($_GET['field']) : "";
        $dir = isset($_GET['dir']) ? trim($_GET['dir']) : "asc";
        $q = "1=1 ";
        $url = "";
        if (isset($s_data['a_date'])) {
            $q.= " and created_at like '%".$s_data['a_date']."%'";
            $url.= "&a_date=".$s_data['a_date'];
        } else $s_data['a_date'] = "";
        $assignments = Assignment::whereRaw($q);
        if (isset($s_data['s_name'])) {
            $assignments = $assignments->whereHas('students', function($student) use ($s_data) {
                return $student->where('fname', 'like', "%" . $s_data['s_name'] . "%")
                ->orwhere('lname', 'like', "%" . $s_data['s_name'] . "%");
            });
            $url.= "&s_name=".$s_data['s_name'];
        }
        else {
            $s_data['s_name'] = "";
        }
        if (isset($s_data['t_name'])) {
            $assignments = $assignments->whereHas('tutors', function($tutor) use ($s_data) {
                return $tutor->where('fname', 'like', "%" . $s_data['t_name'] . "%")
                ->orwhere('lname', 'like', "%" . $s_data['t_name'] . "%");
            });
            $url.= "&t_name=".$s_data['t_name'];
        }
        else {
            $s_data['t_name'] = "";
        }
        $assignments = $assignments->get();
        $objs = [];
        foreach ($assignments as $assignment) {            
            $obj = [];
            $obj['id'] = $assignment->id;
            $obj['created_at'] = $assignment->created_at;
            if ($assignment->student()) {
                $obj['student_name'] = $assignment->student()['fname']. " ". $assignment->student()['lname'] ;
            }
            else {
                $obj['student_name'] = "" ;
            }
            if ($assignment->tutor()) {
                $obj['tutor_name'] = $assignment->tutor()['fname']. " ". $assignment->tutor()['lname'] ;
            }
            else {
                $obj['tutor_name'] = "" ;
            }
            $subjects = "";
            foreach ($assignment->subjects()->get() as $subject)
            {
                $subjects .= $subject->name . ', ';
            }
            $subjects = rtrim($subjects, ', ');
            $obj['subjects'] = $subjects;
            $objs[]=$obj;
        }
        if ($field!="") {
            $fields  = array_column($objs, $field);
            if ($dir == 'asc') {
                array_multisort($fields, SORT_ASC, $objs);
            }
            else {
                array_multisort($fields, SORT_DESC, $objs);
            }
        }      
        if ($url !="") {
            $url = substr($url, 1);
        }
        $data = [
            'assignments'   => $objs,
            'search'        => $s_data,
            'url'           => $url,
            'order'  => [
                'field' => $field,
                'dir' => $dir
            ],
        ];
        if( count($assignments) != 0 )
        {
            return view('admin.assignments.index')->with('data', $data);
        }
        if (count($assignments) == 0) {
            session()->flash('error', "No search results!");
        }
        return view('admin.assignments.index')->with('data', $data);
    }
public function index ( Request $ request ) { $ s_data = $ this -> validate ( $ request , [ 's_name' => 'nullable|string' , 't_name' => 'nullable|string' , 'a_date' => 'nullable|date' , ] ) ; $ field = isset ( $ _GET [ 'field' ] ) ? trim ( $ _GET [ 'field' ] ) : " " ; $ dir = isset ( $ _GET [ 'dir' ] ) ? trim ( $ _GET [ 'dir' ] ) : " asc " ; $ q = " 1=1  " ; $ url = " " ; if ( isset ( $ s_data [ 'a_date' ] ) ) { $ q .= "  and created_at like '% " . $ s_data [ 'a_date' ] . " %' " ; $ url .= " &a_date= " . $ s_data [ 'a_date' ] ; } else $ s_data [ 'a_date' ] = " " ; $ assignments = Assignment :: whereRaw ( $ q ) ; if ( isset ( $ s_data [ 's_name' ] ) ) { $ assignments = $ assignments -> whereHas ( 'students' , function ( $ student ) use ( $ s_data ) { return $ student -> where ( 'fname' , 'like' , " % " . $ s_data [ 's_name' ] . " % " ) -> orwhere ( 'lname' , 'like' , " % " . $ s_data [ 's_name' ] . " % " ) ; } ) ; $ url .= " &s_name= " . $ s_data [ 's_name' ] ; } else { $ s_data [ 's_name' ] = " " ; } if ( isset ( $ s_data [ 't_name' ] ) ) { $ assignments = $ assignments -> whereHas ( 'tutors' , function ( $ tutor ) use ( $ s_data ) { return $ tutor -> where ( 'fname' , 'like' , " % " . $ s_data [ 't_name' ] . " % " ) -> orwhere ( 'lname' , 'like' , " % " . $ s_data [ 't_name' ] . " % " ) ; } ) ; $ url .= " &t_name= " . $ s_data [ 't_name' ] ; } else { $ s_data [ 't_name' ] = " " ; } $ assignments = $ assignments -> get ( ) ; $ objs = [ ] ; foreach ( $ assignments as $ assignment ) { $ obj = [ ] ; $ obj [ 'id' ] = $ assignment -> id ; $ obj [ 'created_at' ] = $ assignment -> created_at ; if ( $ assignment -> student ( ) ) { $ obj [ 'student_name' ] = $ assignment -> student ( ) [ 'fname' ] . " " . $ assignment -> student ( ) [ 'lname' ] ; } else { $ obj [ 'student_name' ] = " " ; } if ( $ assignment -> tutor ( ) ) { $ obj [ 'tutor_name' ] = $ assignment -> tutor ( ) [ 'fname' ] . " " . $ assignment -> tutor ( ) [ 'lname' ] ; } else { $ obj [ 'tutor_name' ] = " " ; } $ subjects = " " ; foreach ( $ assignment -> subjects ( ) -> get ( ) as $ subject ) { $ subjects .= $ subject -> name . ', ' ; } $ subjects = rtrim ( $ subjects , ', ' ) ; $ obj [ 'subjects' ] = $ subjects ; $ objs [ ] = $ obj ; } if ( $ field != " " ) { $ fields = array_column ( $ objs , $ field ) ; if ( $ dir == 'asc' ) { array_multisort ( $ fields , SORT_ASC , $ objs ) ; } else { array_multisort ( $ fields , SORT_DESC , $ objs ) ; } } if ( $ url != " " ) { $ url = substr ( $ url , 1 ) ; } $ data = [ 'assignments' => $ objs , 'search' => $ s_data , 'url' => $ url , 'order' => [ 'field' => $ field , 'dir' => $ dir ] , ] ; if ( count ( $ assignments ) != 0 ) { return view ( 'admin.assignments.index' ) -> with ( 'data' , $ data ) ; } if ( count ( $ assignments ) == 0 ) { session ( ) -> flash ( 'error' , " No search results! " ) ; } return view ( 'admin.assignments.index' ) -> with ( 'data' , $ data ) ; }
Display a listing of the resource.
Display a listing of the resource .
// $s_name = isset($_GET['s_name']) ? trim($_GET['s_name']) : ""; // $t_name =  isset($_GET['t_name']) ? trim($_GET['t_name']) : ""; // $a_id = isset($_GET['a_id']) ? trim($_GET['a_id']) : ""; // $a_date = isset($_GET['a_date']) ? trim($_GET['a_date']) : ""; // dd( $assignments->toSql());