app/Http/Controllers/Instructor/Courses/CourseController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $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',
            'image' => 'required|mimes:jpg,png,jpeg,webp|between:1,6000',
            'course_overview_provider' => 'required',
            'course_overview_url' => 'required',
            'meta_keywords' => 'required|max:255',
            'meta_description' => 'required|max:120', 
            'requirements' => 'required|array|min:1',
            'requirements.*.description' => 'required|max:255',
            'outcomes' => 'required|array|min:1',
            'outcomes.*.description' => 'required|min:1',
            'whos' => 'required|array|min:1',
            'whos.*.description' => 'required|min:1'
        ]);
        $requirements = [];
        foreach ($request->requirements as $requirement)
        {
            $requirements[] = new CourseRequirement($requirement);
        }
        $outcomes = [];
        foreach ($request->outcomes as $outcome)
        {
            $outcomes[] = new CourseOutcome($outcome);
        }
        $whos = [];
        foreach ($request->whos as $who)
        {
            $whos[] = new CourseWho($who);
        }
        $course = new Course($request->all());
        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;
        }
        $course->slug = str_slug($request->title, '-');
        if (isset($request->image)) {
            $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->image_public_id = Cloudder::getPublicId();
        }
        $request->user()
            ->courses()->save($course);
        $image->move(public_path('uploads'), $name);
        $course->requirements()
            ->saveMany($requirements);
        $course->outcomes()
            ->saveMany($outcomes);
        $course->whos()
            ->saveMany($whos);
        return response()
            ->json([
                'saved' => true,
                'id' => $course->id,
                'message' => 'You have successfully uploaded your course!'
            ]);
    }
public function store ( Request $ request ) { $ 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' , 'image' => 'required|mimes:jpg,png,jpeg,webp|between:1,6000' , 'course_overview_provider' => 'required' , 'course_overview_url' => 'required' , 'meta_keywords' => 'required|max:255' , 'meta_description' => 'required|max:120' , 'requirements' => 'required|array|min:1' , 'requirements.*.description' => 'required|max:255' , 'outcomes' => 'required|array|min:1' , 'outcomes.*.description' => 'required|min:1' , 'whos' => 'required|array|min:1' , 'whos.*.description' => 'required|min:1' ] ) ; $ requirements = [ ] ; foreach ( $ request -> requirements as $ requirement ) { $ requirements [ ] = new CourseRequirement ( $ requirement ) ; } $ outcomes = [ ] ; foreach ( $ request -> outcomes as $ outcome ) { $ outcomes [ ] = new CourseOutcome ( $ outcome ) ; } $ whos = [ ] ; foreach ( $ request -> whos as $ who ) { $ whos [ ] = new CourseWho ( $ who ) ; } $ course = new Course ( $ request -> all ( ) ) ; 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 ; } $ course -> slug = str_slug ( $ request -> title , '-' ) ; if ( isset ( $ request -> image ) ) { $ 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 -> image_public_id = Cloudder :: getPublicId ( ) ; } $ request -> user ( ) -> courses ( ) -> save ( $ course ) ; $ image -> move ( public_path ( 'uploads' ) , $ name ) ; $ course -> requirements ( ) -> saveMany ( $ requirements ) ; $ course -> outcomes ( ) -> saveMany ( $ outcomes ) ; $ course -> whos ( ) -> saveMany ( $ whos ) ; return response ( ) -> json ( [ 'saved' => true , 'id' => $ course -> id , 'message' => 'You have successfully uploaded your course!' ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// Save Multiple Requirements // End // Save Multiple Outcomes // End // Save Multiple For who?
 // End // Check if booleans are true or false // Slug // Cloud Upload using Cloudinary API // End // Save to Public path