app/Http/Controllers/Instructor/Courses/CourseController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required|string|max:255',
            'excerpt' => 'required|string|max:150',
            'description' => 'required|string',
            'category_id' => 'required',
            'level' => 'required',
            'price' => 'present',
            'discount' => 'present',
            'course_overview_provider' => 'required',
            'course_overview_url' => 'required',
            'meta_keywords' => 'required|max:255',
            'meta_description' => 'required|max:120', 
            'requirements' => 'required|array|min:1',
            'requirements.*.id' => 'integer|exists:course_requirements',
            'requirements.*.description' => 'required',
            'outcomes' => 'required|array|min:1',
            'outcomes.*.id' => 'integer|exists:course_outcomes',
            'outcomes.*.description' => 'required',
            'whos' => 'required|array|min:1',
            'whos.*.id' => 'integer|exists:course_whos',
            'whos.*.description' => 'required'
        ]);
        $course = Course::where('id', $id)
            ->where('teacher_id', Auth::user()->id)
            ->findOrFail($id);
        $input = $request->all();
        $course->fill($input);
        if (isset($request->discount)) {
            $course->has_discount = 1;
        } else {
            $course->has_discount = 0;
        }
        if (isset($request->price)) {
            $course->free_course = 0;
        } else {
            $course->free_course = 1;
        }
        $requirements = [];
        $requirementsUpdated = [];
        foreach ($request->requirements as $requirement) {
            if (isset($requirement['id'])) {
                CourseRequirement::where('course_id', $course->id)
                    ->where('id', $requirement['id'])
                    ->update($requirement);
                $requirementsUpdated[] = $requirement['id'];
            } else {
                $requirements[] = new CourseRequirement($requirement);
            }
        }
        $outcomes = [];
        $outcomesUpdated = [];
        foreach ($request->outcomes as $outcome) {
            if (isset($outcome['id'])) {
                CourseOutcome::where('course_id', $course->id)
                    ->where('id', $outcome['id'])
                    ->update($outcome);
                $outcomesUpdated[] = $outcome['id'];
            } else {
                $outcomes[] = new CourseOutcome($outcome);
            }
        }
        $whos = [];
        $whosUpdated = [];
        foreach ($request->whos as $who) {
            if (isset($who['id'])) {
                CourseWho::where('course_id', $course->id)
                    ->where('id', $who['id'])
                    ->update($who);
                $whosUpdated[] = $who['id'];
            } else {
                $whos[] = new CourseWho($who);
            }
        }
        $course->slug = str_slug($request->title, '-');
        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $image = $request->file('image');
            $name = $request->file('image')->getClientOriginalName();
            $image_name = $request->file('image')->getRealPath();
            Cloudder::upload($image_name, null);
            list($width, $height) = getimagesize($image_name);
            $image_url = Cloudder::show(Cloudder::getPublicId(), ['width' => $width, 'height' => $height]);
            $course->image = $image_url;
        }
        $course->save();
        CourseRequirement::whereNotIn('id', $requirementsUpdated)
            ->where('course_id', $course->id)
            ->delete();
        CourseOutcome::whereNotIn('id', $outcomesUpdated)
            ->where('course_id', $course->id)
            ->delete();
        CourseWho::whereNotIn('id', $whosUpdated)
            ->where('course_id', $course->id)
            ->delete();
        if (count($requirements)) {
            $course->requirements()
                ->saveMany($requirements);
        }
        if (count($outcomes)) {
            $course->outcomes()
                ->saveMany($outcomes);
        }
        if (count($whos)) {
            $course->whos()
                ->saveMany($whos);
        }
        return response()
            ->json([
                'updated' => true,
                'id' => $course->id,
                'message' => 'You have successfully updated your course!'
            ]);
    }
public function update ( Request $ request , $ id ) { $ this -> validate ( $ request , [ 'title' => 'required|string|max:255' , 'excerpt' => 'required|string|max:150' , 'description' => 'required|string' , 'category_id' => 'required' , 'level' => 'required' , 'price' => 'present' , 'discount' => 'present' , 'course_overview_provider' => 'required' , 'course_overview_url' => 'required' , 'meta_keywords' => 'required|max:255' , 'meta_description' => 'required|max:120' , 'requirements' => 'required|array|min:1' , 'requirements.*.id' => 'integer|exists:course_requirements' , 'requirements.*.description' => 'required' , 'outcomes' => 'required|array|min:1' , 'outcomes.*.id' => 'integer|exists:course_outcomes' , 'outcomes.*.description' => 'required' , 'whos' => 'required|array|min:1' , 'whos.*.id' => 'integer|exists:course_whos' , 'whos.*.description' => 'required' ] ) ; $ course = Course :: where ( 'id' , $ id ) -> where ( 'teacher_id' , Auth :: user ( ) -> id ) -> findOrFail ( $ id ) ; $ input = $ request -> all ( ) ; $ course -> fill ( $ input ) ; if ( isset ( $ request -> discount ) ) { $ course -> has_discount = 1 ; } else { $ course -> has_discount = 0 ; } if ( isset ( $ request -> price ) ) { $ course -> free_course = 0 ; } else { $ course -> free_course = 1 ; } $ requirements = [ ] ; $ requirementsUpdated = [ ] ; foreach ( $ request -> requirements as $ requirement ) { if ( isset ( $ requirement [ 'id' ] ) ) { CourseRequirement :: where ( 'course_id' , $ course -> id ) -> where ( 'id' , $ requirement [ 'id' ] ) -> update ( $ requirement ) ; $ requirementsUpdated [ ] = $ requirement [ 'id' ] ; } else { $ requirements [ ] = new CourseRequirement ( $ requirement ) ; } } $ outcomes = [ ] ; $ outcomesUpdated = [ ] ; foreach ( $ request -> outcomes as $ outcome ) { if ( isset ( $ outcome [ 'id' ] ) ) { CourseOutcome :: where ( 'course_id' , $ course -> id ) -> where ( 'id' , $ outcome [ 'id' ] ) -> update ( $ outcome ) ; $ outcomesUpdated [ ] = $ outcome [ 'id' ] ; } else { $ outcomes [ ] = new CourseOutcome ( $ outcome ) ; } } $ whos = [ ] ; $ whosUpdated = [ ] ; foreach ( $ request -> whos as $ who ) { if ( isset ( $ who [ 'id' ] ) ) { CourseWho :: where ( 'course_id' , $ course -> id ) -> where ( 'id' , $ who [ 'id' ] ) -> update ( $ who ) ; $ whosUpdated [ ] = $ who [ 'id' ] ; } else { $ whos [ ] = new CourseWho ( $ who ) ; } } $ course -> slug = str_slug ( $ request -> title , '-' ) ; if ( $ request -> hasFile ( 'image' ) && $ request -> file ( 'image' ) -> isValid ( ) ) { $ image = $ request -> file ( 'image' ) ; $ name = $ request -> file ( 'image' ) -> getClientOriginalName ( ) ; $ image_name = $ request -> file ( 'image' ) -> getRealPath ( ) ; Cloudder :: upload ( $ image_name , null ) ; list ( $ width , $ height ) = getimagesize ( $ image_name ) ; $ image_url = Cloudder :: show ( Cloudder :: getPublicId ( ) , [ 'width' => $ width , 'height' => $ height ] ) ; $ course -> image = $ image_url ; } $ course -> save ( ) ; CourseRequirement :: whereNotIn ( 'id' , $ requirementsUpdated ) -> where ( 'course_id' , $ course -> id ) -> delete ( ) ; CourseOutcome :: whereNotIn ( 'id' , $ outcomesUpdated ) -> where ( 'course_id' , $ course -> id ) -> delete ( ) ; CourseWho :: whereNotIn ( 'id' , $ whosUpdated ) -> where ( 'course_id' , $ course -> id ) -> delete ( ) ; if ( count ( $ requirements ) ) { $ course -> requirements ( ) -> saveMany ( $ requirements ) ; } if ( count ( $ outcomes ) ) { $ course -> outcomes ( ) -> saveMany ( $ outcomes ) ; } if ( count ( $ whos ) ) { $ course -> whos ( ) -> saveMany ( $ whos ) ; } return response ( ) -> json ( [ 'updated' => true , 'id' => $ course -> id , 'message' => 'You have successfully updated your course!' ] ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// Check if booleans are true or false // Slug // Cloud Upload/Update using Cloudinary API