在用户配置文件中,我显示页面打开时打开的项目到页面的 show() 方法,如果我编辑用户配置文件并且页面重新加载,它表示没有显示项目的方法,因为编辑() 方法被触发进行编辑,我必须复制一段代码以在编辑方法中显示项目。可以做些什么来避免在不同的方法中重复相同的代码?
public function show()
{
//
$shoolsubject = DB::table('user_subjects')
->join('shool_targets', 'shool_targets.id', '=', 'user_subjects.target')
->join('school_subjects', 'school_subjects.id', '=', 'user_subjects.shool')
->select('shool_targets.name as st', 'school_subjects.name as ss')
->where('user_subjects.user_id', Auth::user()->id)->get();
return view('profile.profile')->with(['profile' => Auth::user(), 'shool' => $shoolsubject->all()]);
}
public function edit(Request $request)
{
/*
* Вывод добавленных предметов
*/
$shoolsubject = DB::table('user_subjects')
->join('shool_targets', 'shool_targets.id', '=', 'user_subjects.target')
->join('school_subjects', 'school_subjects.id', '=', 'user_subjects.shool')
->select('shool_targets.name as st', 'school_subjects.name as ss')
->where('user_subjects.user_id', Auth::user()->id)->get();
return view('profile.profile')->with(['profile' => Auth::user(), 'shool' => $shoolsubject->all()]);
}
1 个回答