app/Http/Controllers/ClassController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Classes  $classes
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $slug)
    {
        $model = Classes::where('slug', $slug)->first();
        if ($model == null) {
            return redirect()->route('class');
        }
        $rules = Classes::RULES_FOR_EDIT;
        $rules['name'][] = Rule::unique('classes')->ignore($slug, 'slug');
        $validattionErrorMessages = Classes::VALIDATION_ERROR_MESSAGES;
        $validator = Validator::make($request->all(), $rules, $validattionErrorMessages);
        if ($validator->fails()) {
            return redirect()->route('class.edit', ['slug' => $slug])
                            ->withErrors($validator->errors())
                            ->withInput();
        } else {
            $columnsInTable = \Illuminate\Support\Facades\Schema::getColumnListing('classes');
            foreach ($request->all() as $key => $value) {
                if(in_array($key, $columnsInTable)){
                    $model->$key = $value;
                }
            }
            $model->slug = Str::random(6) . '-' . Str::slug($model->name);
            if ($model->save()) {
                session()->flash('successful', 'Sửa thành công');
            } else {
                session()->flash('error', 'Thất bại, vui lòng thử lại');
            }
            return redirect()->route('class');
        }
    }
public function update ( Request $ request , $ slug ) { $ model = Classes :: where ( 'slug' , $ slug ) -> first ( ) ; if ( $ model == null ) { return redirect ( ) -> route ( 'class' ) ; } $ rules = Classes :: RULES_FOR_EDIT ; $ rules [ 'name' ] [ ] = Rule :: unique ( 'classes' ) -> ignore ( $ slug , 'slug' ) ; $ validattionErrorMessages = Classes :: VALIDATION_ERROR_MESSAGES ; $ validator = Validator :: make ( $ request -> all ( ) , $ rules , $ validattionErrorMessages ) ; if ( $ validator -> fails ( ) ) { return redirect ( ) -> route ( 'class.edit' , [ 'slug' => $ slug ] ) -> withErrors ( $ validator -> errors ( ) ) -> withInput ( ) ; } else { $ columnsInTable = \ Illuminate \ Support \ Facades \ Schema :: getColumnListing ( 'classes' ) ; foreach ( $ request -> all ( ) as $ key => $ value ) { if ( in_array ( $ key , $ columnsInTable ) ) { $ model -> $ key = $ value ; } } $ model -> slug = Str :: random ( 6 ) . '-' . Str :: slug ( $ model -> name ) ; if ( $ model -> save ( ) ) { session ( ) -> flash ( 'successful' , 'Sửa thành công'); } else { session ( ) -> flash ( 'error' , 'Thất bại, vui lòng thử lại'); } return redirect ( ) -> route ( 'class' ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .
/**
         * if the item has been removed
         */ /**
         *
         */ /**
             * save on database
             */ //