app/Http/Controllers/CourseController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Course  $course
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $data = $request->all();
        $rules = [
            "name" => "string",
            "certificate" => "boolean",
            "thumbnail" => "string|url",
            "type" => "in:free,premium",
            "status" => "in:draft,published",
            "price" => "integer",
            "level" => "in:all-level,beginner,intermediate,advance",
            "mentor_id" => "integer",
            "description" => "string"
        ];
        $validator = Validator::make($data, $rules);
        if ($validator->fails()) {
            return response()->json([
                "status" => "error",
                "message" => $validator->errors()
            ], 400);
        }
        $course = Course::find($id);
        if (!$course) {
            return response()->json([
                "status" => "error",
                "message" => "course not found"
            ], 404);
        }
        $mentor_id = $request->mentor_id;
        if ($mentor_id) {
            $mentor = Mentor::find($mentor_id);
            if (!$mentor) {
                return response()->json([
                    "status" => "error",
                    "message" => "mentor not found"
                ], 404);
            }
        }
        $course->update($data);
        return response()->json([
            "status" => "success",
            "data" => $course
        ]);
    }
public function update ( Request $ request , $ id ) { $ data = $ request -> all ( ) ; $ rules = [ " name " => " string " , " certificate " => " boolean " , " thumbnail " => " string|url " , " type " => " in:free,premium " , " status " => " in:draft,published " , " price " => " integer " , " level " => " in:all-level,beginner,intermediate,advance " , " mentor_id " => " integer " , " description " => " string " ] ; $ validator = Validator :: make ( $ data , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ " status " => " error " , " message " => $ validator -> errors ( ) ] , 400 ) ; } $ course = Course :: find ( $ id ) ; if ( ! $ course ) { return response ( ) -> json ( [ " status " => " error " , " message " => " course not found " ] , 404 ) ; } $ mentor_id = $ request -> mentor_id ; if ( $ mentor_id ) { $ mentor = Mentor :: find ( $ mentor_id ) ; if ( ! $ mentor ) { return response ( ) -> json ( [ " status " => " error " , " message " => " mentor not found " ] , 404 ) ; } } $ course -> update ( $ data ) ; return response ( ) -> json ( [ " status " => " success " , " data " => $ course ] ) ; }
Update the specified resource in storage.
Update the specified resource in storage .