Example #1
0
 public function update(Course $course, Request $request)
 {
     $user = $this->user;
     $this->validate($request, ['title' => 'required|min:3', 'active' => 'required|in:0,1', 'description' => 'required|min:3', 'categories.*' => 'integer', 'price' => 'required|integer', 'image' => 'mimes:jpeg,bmp,png,jpg|max:1024']);
     $input = $request->all();
     $previousImage = $course->image;
     /* check if the user has uploaded image or not */
     if ($request->hasFile('image')) {
         $image = $input['image'];
         $imageName = $user->id . str_random(20) . '.' . $image->getClientOriginalExtension();
         $dbImageName = $user->id . '/' . $imageName;
         $image->move(public_path() . '/img/files/' . $user->id, $imageName);
         if ($previousImage != null) {
             if (File::exists(public_path() . '/img/files/' . $previousImage)) {
                 unlink(public_path() . '/img/files/' . $previousImage);
             }
         }
     } else {
         $dbImageName = $previousImage;
     }
     /*create course*/
     $course->update(['title' => $input['title'], 'description' => $input['description'], 'active' => $input['active'], 'price' => $input['price'], 'image' => $dbImageName]);
     /*register subCategories*/
     $selectedCategories = $this->registerSubCategories($request);
     if (!$selectedCategories) {
         //if the selected tags has error
         return redirect()->back();
     }
     $course->categories()->sync($selectedCategories);
     /*register tags*/
     $selected = $this->registerTags($request);
     if (!$selected) {
         //if the selected tags has error
         return redirect()->back();
     }
     $course->tags()->sync($selected);
     Flash::success(trans('users.courseUpdated'));
     return redirect()->back();
 }