1

I am making a website for college administration where professors log in and assign marks to the students they are teaching.

There's a table, called "IA_Marks" in my database:

|Student_ID|Subject_Code|Name|Marks1|Marks2|Marks3|Semester|Division|

There's also a table called "Classroom_Mapper" in my database, that helps map a professor to a classroom, with a subject:

|Prof_ID|Subject_Code|Semester|Division|

This is a method in my controller:

public function showTable(){
    $sem = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('semester');
    $division = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('division');
    $data = DB::table('iamarks')->where([['semester','=',$sem],['division','=',$division]])->get();
    return view('ia',compact('data'));
}

Using this, I can fetch rows that belong to the professor who has logged in.


But, there's a problem.

Say the professor teaches two subjects, in two semesters. Then, the where clause will return multiple results from the mapper table. For example:

select semester from classroom_mapper where Prof_ID=auth()->user()->Prof_ID

output:

8

5

Then the students from both 5th and 8th semester will be shown on his dashboard. Our target semester was, say 5th. Then it'll be a problem.

Registering for a subject, is done as shown here: form screenshot

Let's call the subject being registered in the screenshot "SUBJECT 4". It is a subject for the 5th semester, division A.

I want to dynamically make a button(SUBJECT 4) on the dashboard, which when clicked, sends the semester(5) and division(A) of choice to the controller. Dashboard Screenshot

This button should open a newly made page with name of the subject(subject4.blade.php), where the database table contents for target semester and division(5 and A) will be shown.

How do I make this dynamic view creating button which sends specific info to controller? Is it even possible?

4

1 回答 1

1

使用 Laravel 有几种方法可以做到这一点,但我的 goto 通常是为每个可以动态填充的视图(仪表板、主题等)创建一个刀片模板——假设每个主题视图的布局是相同的。

在您的仪表板视图中,您可以为使用如下格式的每个按钮生成一个 url:http: //cas.jce.in/subject/semester/5/division/a/

接下来,创建一个使用几个参数的路由,如下所示:

Route::get('/subject/semester/{semester_id}/division/{division_id}', 'ControllerName@showSubject');

更多信息:https ://laravel.com/docs/5.8/routing#required-parameters

然后在你的控制器中,添加一个 showSemester 函数,如下所示:

function showSubject($semester_id, $division_id){
    $data = DB::table('table_name')->where('semester', '=', $semester_id)->where('division', '=', $division_id)->first();
    return view('subject', ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
}

您的路由参数按出现顺序可供控制器使用。所以我们可以添加 $semester_id 和 $division_id 作为我们函数的前两个参数。接下来,在将所有内容返回到视图之前,我们将在数据库中检索我们需要的数据。

请注意,我们使用的是单个视图,而不是动态选择一个。您可以为每个主题创建单独的视图,但我认为您可能不需要,除非每个主题的布局在某种程度上都是独一无二的。在这种情况下,你可以简单地做这样的事情,但我通常会尽量避免它。

  $view = 'subject'.$data->subject_id;
  return view($view, ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);

另外,只是一个简短的说明......您可以考虑从上面调整您的数据库查询以使用 select 语句而不是 pluck。最终结果是相同的,但是使用 select 可以通过仅加载您想要的数据来提高性能......而不是预先加载所有内容并将大部分内容丢弃。

$sem = DB::table('classroom_mappers')->where('Prof_ID', Auth()->user()->PID)->pluck('semester');

……变成……

$sem = DB::table('classroom_mappers')->select('semester')->where('Prof_ID', auth()->user()->PID)->get();
于 2019-03-26T19:46:49.657 回答