Example #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', 'مشکل در اتصال به سرور. لطفا مجددا تلاش کنید.');
 }
Example #2
0
 public function store(Request $request)
 {
     $user = Sentinel::getUser()->id;
     $rules = ['title' => 'required|min:5|max:150|unique:samples,title,null,id,user_id,' . $user, 'content' => 'required|min:10', 'img' => 'required|mimes:jpg,png,gif,jpeg,bmp', 'tags' => 'required'];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return back()->withInput()->withErrors($validator);
     } else {
         if ($request->file('img')->isValid()) {
             $image = $request->file('img');
             $filename = $user . "_" . time() . '.' . $image->getClientOriginalExtension();
             $thumbname = 'thumb_' . $filename;
             # Save Img
             Image::make($image)->resize(800, null, function ($constraint) {
                 $constraint->aspectRatio();
                 $constraint->upsize();
             })->save(storage_path('app/samples/org/') . $filename);
             # Save Thumb
             Image::make($image)->resize(200, null, function ($constraint) {
                 $constraint->aspectRatio();
                 $constraint->upsize();
             })->save(storage_path('app/samples/thumb/') . $thumbname);
             # Create Sample
             $sample = new Sample();
             $sample->title = $request->title;
             $sample->content = $request->content;
             $sample->active = 0;
             $sample->img = $filename;
             $sample->category_id = $request->categories;
             $sample->user_id = $user;
             if ($sample->save()) {
                 $tags = $request->tags;
                 foreach ($tags as $tag) {
                     if (is_numeric($tag)) {
                         $tagableSave = new Taggable();
                         $tagableSave->taggable_id = $sample->id;
                         $tagableSave->tag_id = $tag;
                         $tagableSave->taggable_type = 'App\\Sample';
                         $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();
                                 $tagableSave = new Taggable();
                                 $tagableSave->taggable_id = $sample->id;
                                 $tagableSave->tag_id = $tagSave->id;
                                 $tagableSave->taggable_type = 'App\\Sample';
                                 $tagableSave->user_id = $user;
                                 $tagableSave->save();
                             }
                         }
                     }
                 }
                 # Redirect on Success
                 return redirect()->route('user.sample.index')->with('success', trans('validation.sample_success'));
             }
         }
     }
     return back()->withInput()->with('error', 'مشکل در اتصال به سرور. لطفا مجددا تلاش کنید.');
 }