app/Http/Controllers/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)
    {
        $data = $request->all();
        $rules = [
            "name" => "required|string",
            "certificate" => "required|boolean",
            "thumbnail" => "string|url",
            "type" => "required|in:free,premium",
            "status" => "required|in:draft,published",
            "price" => "integer",
            "level" => "required|in:all-level,beginner,intermediate,advance",
            "mentor_id" => "required|integer",
            "description" => "string"
        ];
        $validator = Validator::make($data, $rules);
        if ($validator->fails()) {
            return response()->json([
                "status" => "error",
                "message" => $validator->errors()
            ]);
        }
        $mentor = Mentor::find($request->mentor_id);
        if (!$mentor) {
            return response()->json([
                "status" => "error",
                "message" => "mentor not found"
            ]);
        }
        $course = Course::create($data);
        return response()->json([
            "status" => "success",
            "data" => $course
        ]);
    }
public function store ( Request $ request ) { $ data = $ request -> all ( ) ; $ rules = [ " name " => " required|string " , " certificate " => " required|boolean " , " thumbnail " => " string|url " , " type " => " required|in:free,premium " , " status " => " required|in:draft,published " , " price " => " integer " , " level " => " required|in:all-level,beginner,intermediate,advance " , " mentor_id " => " required|integer " , " description " => " string " ] ; $ validator = Validator :: make ( $ data , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ " status " => " error " , " message " => $ validator -> errors ( ) ] ) ; } $ mentor = Mentor :: find ( $ request -> mentor_id ) ; if ( ! $ mentor ) { return response ( ) -> json ( [ " status " => " error " , " message " => " mentor not found " ] ) ; } $ course = Course :: create ( $ data ) ; return response ( ) -> json ( [ " status " => " success " , " data " => $ course ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .