Last active
March 13, 2020 01:24
-
-
Save robertnicjoo/54aa295e704b66b23814ceeedb82b072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Http\Request; | |
use App\School; | |
use App\SchoolSemester; | |
use App\SemesterClass; | |
use App\ClassStudent; | |
use DB; | |
class StudentsController extends Controller | |
{ | |
public function indexData($id) | |
{ | |
$students = DB::table('schools') | |
->where('schools.id', $id) | |
->join('school_semesters', 'school_semesters.school_id', '=', 'schools.id') | |
->join('semester_classes', 'semester_classes.semester_id', '=', 'school_semesters.id') | |
->join('class_students', 'class_students.class_id', '=', 'semester_classes.id') | |
->join('users', 'users.id', '=', 'class_students.user_id') | |
->join('user_profiles', 'users.id', '=', 'user_profiles.user_id') | |
->select( | |
\DB::raw('group_concat(DISTINCT school_semesters.name) as semester'), | |
\DB::raw('group_concat(DISTINCT semester_classes.name) as class'), | |
'users.name as students', | |
'user_profiles.photo as photo', | |
'users.id as id' | |
) | |
->groupBy('users.id') | |
->get(); | |
return datatables()->of($students) | |
->escapeColumns(['action']) | |
->setRowId('id') | |
->addColumn( 'photo', function ( $student ) { | |
return [ | |
'<a target="_blank" href="' . url('images') . '/' . $student->photo . '"><img src="' . url('images') . '/' . $student->photo . '" alt="' . $student->students . '" width="50" height="50" /></a>' | |
]; | |
}) | |
->addColumn( 'students', function ( $student ) { | |
return [ | |
'<a href="#" data-type="text" data-title="Change Student Name" data-name="name" data-url="'. route('userAjaxUpdate').'" data-pk="'.$student->id.'" class="username">'.$student->students.'</a>' | |
]; | |
}) | |
->addColumn( 'action', function ( $student ) { | |
if(!empty($student->deleted_at)) { | |
$mm = '<form style="display: inline;" action="'. route('usersRestore', $student->id) .'" method="POST"> | |
<input type="hidden" name="_method" value="POST"> | |
<input type="hidden" name="_token" value="'. csrf_token() .'"> | |
<button type="submit" class="btn btn-xs btn-warning"> | |
<i class="fa text-white fa-recycle"></i> Restore</button> | |
</form>'; | |
} else { | |
$mm = '<form style="display: inline;" action="'. route('users.destroy', $student->id) .'" method="POST"> | |
<input type="hidden" name="_method" value="DELETE"> | |
<input type="hidden" name="_token" value="'. csrf_token() .'"> | |
<button type="submit" class="btn btn-xs btn-danger"> | |
<i class="fa text-white fa-trash"></i> Remove</button> | |
</form>'; | |
} | |
return [ | |
'<a href="' . route( 'users.show', $student->id ) . '" class="btn btn-xs btn-info"><i class="fa text-white fa-eye"></i> Show</a>', | |
'<a href="' . route( 'users.edit', $student->id ) . '" class="btn btn-xs btn-primary"><i class="fa text-white fa-pencil"></i> Edit</a>', | |
$mm | |
]; | |
}) | |
->toJson(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment