public function postEdit(Request $request) { $update = false; $this->validate($request, ["first_name" => "required|alpha|min:2|max:50", "last_name" => "required|alpha|min:2|max:50", "location" => "max:20", "timezone" => "required|timezone", "facebook_url" => "url", "google_url" => "url", "linkedin_url" => "url"]); $skills = (array) explode(",", $request->input('skills')); $skills = array_filter(array_unique($skills)); $validator = Validator::make(['skills' => count($skills)], ["skills" => "max:20"]); if ($validator->fails()) { return redirect()->back(); } //Detach all skill not have in new list $userSkills = Auth::user()->skills()->get(); foreach ($userSkills as $value) { if (!in_array($value->id, $skills)) { Auth::user()->skills()->detach(['skill_id' => $value->id]); $update = true; } } //Add new skill to user skill if that skill not yet have in user skill list foreach ($skills as $value) { $haveSkill = Auth::user()->skills()->find($value); if (!$haveSkill) { $skill = Skill::find($value); if ($skill) { Auth::user()->skills()->attach(['skill_id' => $value]); $update = true; } } } $arrayUpdate = null; if ($request->input('first_name') != Auth::user()->first_name) { $arrayUpdate['first_name'] = $request->input('first_name'); } if ($request->input('last_name') != Auth::user()->last_name) { $arrayUpdate['last_name'] = $request->input('last_name'); } if ($request->input('location') != Auth::user()->location) { $arrayUpdate['location'] = $request->input('location'); } if ($request->input('timezone') != Auth::user()->timezone) { $arrayUpdate['timezone'] = $request->input('timezone'); } if ($request->input('facebook_url') != Auth::user()->facebook_url) { $arrayUpdate['facebook_url'] = $request->input('facebook_url'); } if ($request->input('google_url') != Auth::user()->google_url) { $arrayUpdate['google_url'] = $request->input('google_url'); } if ($request->input('linkedin_url') != Auth::user()->linkedin_url) { $arrayUpdate['linkedin_url'] = $request->input('linkedin_url'); } if ($arrayUpdate) { Auth::user()->update($arrayUpdate); $update = true; } if ($update) { return redirect()->route('profile.edit')->with('success', 'Your profile has been updated.'); } return redirect()->route('profile.edit'); }
public function getMatched(\Illuminate\Http\Request $request) { $skills_old = []; if ($request->old('skills')) { $skills = (array) explode(",", $request->old('skills')); $skills = array_filter(array_unique($skills)); foreach ($skills as $value) { $skill = Skill::find($value); if ($skill) { $skills_old[] = $skill->toArray(); } } } else { $skills_old = Auth::user()->skills()->get()->toArray(); } $arrayId = []; foreach ($skills_old as $key => $value) { $arrayId[] = $value['id']; } $posts = Post::where('active', true)->whereHas('skills', function ($query) use($arrayId) { $query->whereIn('skills.id', $arrayId); })->orderBy('active_at', 'desc')->paginate(30); return view('post.matched')->with('posts', $posts)->with('skills', json_encode($skills_old)); }