public function addBookmark(Request $request) { //scope $current_user = Auth::user(); //create a new bookmark and associate with current user $bookmark = new Bookmark(); $bookmark->user()->associate($current_user->id); //configure the bookmark $bookmark->name = $request->name; $bookmark->url = $request->url; $bookmark->private = $request->private == 'on' ? true : false; //save it $bookmark->save(); //if there are tags assigned... if (!empty($request->tags)) { //lets make an array of the tags $tags = array(); $in_tags = explode(',', $request->tags); //and associate each one to the current bookmark foreach ($in_tags as $in_tag) { $tag = Tag::firstOrNew(['name' => $in_tag, 'user_id' => $current_user->id]); $tag->user()->associate($current_user->id); $tag->save(); array_push($tags, $tag); } //save the tags $bookmark->tags()->saveMany($tags); } //all is well, so pass back a message $message = array('status' => 'OK', 'message' => 'Bookmark added!'); //redirect to the dashboard view with the message return redirect('dashboard')->with('message', $message); }