app/Http/Controllers/ClassController.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)
    {
        $model = new Classes();
        $rules = Classes::RULES_FOR_ADD;
        $validattionErrorMessages = Classes::VALIDATION_ERROR_MESSAGES;
        $validator = Validator::make($request->all(), $rules, $validattionErrorMessages);
        if ($validator->fails()) {
            return redirect()->route('class.create')
                            ->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', 'Thêm mới thành công');
            } else {
                session()->flash('error', 'Thất bại, vui lòng thử lại');
            }
            return redirect()->route('class');
        }
    }
public function store ( Request $ request ) { $ model = new Classes ( ) ; $ rules = Classes :: RULES_FOR_ADD ; $ validattionErrorMessages = Classes :: VALIDATION_ERROR_MESSAGES ; $ validator = Validator :: make ( $ request -> all ( ) , $ rules , $ validattionErrorMessages ) ; if ( $ validator -> fails ( ) ) { return redirect ( ) -> route ( 'class.create' ) -> 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' , 'Thêm mới thành công'); } else { session ( ) -> flash ( 'error' , 'Thất bại, vui lòng thử lại'); } return redirect ( ) -> route ( 'class' ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
// /**
             * save on database
             */ //