Last active
January 20, 2019 21:22
-
-
Save connor11528/e69385e5b5986b3d087db1cb3b35a9ac to your computer and use it in GitHub Desktop.
Laravel backend for adding tags to the Employbl candidate application
This file contains hidden or 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 Illuminate\Http\Request; | |
use App\User; | |
use Spatie\Tags\Tag; | |
class CandidateController extends Controller | |
{ | |
public function edit($id) | |
{ | |
$candidate = auth()->user(); | |
if ($candidate->id != $id) { | |
abort(403, 'Forbidden'); | |
} | |
// Get all the available tags candidates can select | |
$allTags = Tag::getWithType('candidate')->map(function ($tag) { | |
return $tag->only(['name']); | |
})->pluck('name'); | |
$candidate->load([ | |
'tags' | |
]); | |
// Only grab the name of the tags the candidate has (not the whole tag object) | |
$candidateTags = $candidate->tags->map(function($tag) { | |
return $tag->name; | |
}); | |
return view('candidates.edit', compact('candidate', 'allTags', 'candidateTags')); | |
} | |
public function update(Request $request, $id) | |
{ | |
$candidate = auth()->user(); | |
if ($candidate->id != $id) { | |
abort(403, 'Forbidden. You can only update your own profile.'); | |
} | |
$request->validate( | |
[ | |
'summary' => 'nullable|string', | |
] | |
); | |
$user = User::find($id); | |
$user->summary = trim($request->input('summary')); | |
$tags = $request->input('tags'); | |
$user->attachTags($tags); | |
$user->save(); | |
return response()->json( | |
[ | |
'message' => '🎉 You updated your profile!', | |
'candidate' => $user, | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to create a candidate profile on Employbl: https://employbl.com/candidates
Full Medium tutorial here: https://medium.com/employbl/build-tagging-with-vue-multiselect-and-laravel-tags-960c260df438