Пример #1
0
 public function update(Request $request, Blog $blog)
 {
     $user = Sentinel::getUser()->id;
     $rules = ['title' => 'required|min:5|max:150|alpha_num', 'content' => 'required|min:10'];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return back()->withInput()->withErrors($validator);
     } else {
         # Create Blog
         $blog = Blog::find($blog->id);
         $blog->title = $request->title;
         $blog->content = $request->content;
         $blog->user_id = $user;
         # Redirect on Success
         if ($blog->save()) {
             $tags = $request->tags;
             foreach ($tags as $tag) {
                 if (is_numeric($tag)) {
                     $tagabledeletedRows = Taggable::where('tag_id', $tag)->delete();
                     $tagableSave = new Taggable();
                     $tagableSave->taggable_id = $blog->id;
                     $tagableSave->tag_id = $tag;
                     $tagableSave->taggable_type = 'App\\Blog';
                     $tagableSave->user_id = $user;
                     $tagableSave->save();
                 } else {
                     if (strlen($tag) >= 3) {
                         $tagExist = Tag::where('text', $tag)->count();
                         if ($tagExist == 0) {
                             $tagSave = new Tag();
                             $tagSave->text = $tag;
                             $tagSave->save();
                         }
                     }
                 }
             }
             return redirect()->route('user.blog.index')->with('success', trans('validation.blog_success'));
         }
     }
     return back()->withInput()->with('error', 'مشکل در اتصال به سرور. لطفا مجددا تلاش کنید.');
 }