app/Http/Controllers/StudentController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $slug)
    {
        $model = Student::where('slug', $slug)->first();
        if ($model == null) {
            return redirect()->route('student');
        }
        $response = Gate::inspect('update', $model);
        if (!$response->allowed()) {
            session()->flash('permission', $response->message());
            return redirect()->route('student');
        }
        $rules = Student::RULES_FOR_EDIT;
        $validattionErrorMessages = Student::VALIDATION_ERROR_MESSAGES;
        $validator = Validator::make($request->all(), $rules, $validattionErrorMessages);
        if ($validator->fails()) {
            return redirect()->route('student.edit', ['slug' => $slug])
                            ->withErrors($validator->errors())
                            ->withInput();
        } else {
            $columnsInTable = \Illuminate\Support\Facades\Schema::getColumnListing('students');
            foreach ($request->all() as $key => $value) {
                if(in_array($key, $columnsInTable) && $key != 'photo' && $key != 'description'){
                    $model->$key = $value;
                }
            }
            $model->slug = Str::random(6) . '-' . Str::slug($model->name);
            if(!$request->has('interest')){
                $model->interest = null;
            }
            if($request->file('photo')){
                Storage::delete('public/photos/'.$model->photo);
                Storage::delete('public/photos/thumbnail/'.$model->photo);
                $path = Storage::putFile('public/photos', $request->file('photo'));
                $model->photo = str_replace('public/photos/', '', $path);
                $image = $request->file('photo');
                $destinationPath = storage_path('app/public/photos/thumbnail');
                $img = Image::make($image->getRealPath());
                $img->resize(100, 100, function ($constraint) {
                    $constraint->aspectRatio();
                })->save($destinationPath.'/'.$model->photo);
            }
            if($request->file('description')){
                Storage::delete('public/descriptions/'.$model->description);
                $path = Storage::putFile('public/descriptions', $request->file('description'));
                $model->description = str_replace('public/descriptions/', '', $path);
            }
            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('student');
        }
    }
public function update ( Request $ request , $ slug ) { $ model = Student :: where ( 'slug' , $ slug ) -> first ( ) ; if ( $ model == null ) { return redirect ( ) -> route ( 'student' ) ; } $ response = Gate :: inspect ( 'update' , $ model ) ; if ( ! $ response -> allowed ( ) ) { session ( ) -> flash ( 'permission' , $ response -> message ( ) ) ; return redirect ( ) -> route ( 'student' ) ; } $ rules = Student :: RULES_FOR_EDIT ; $ validattionErrorMessages = Student :: VALIDATION_ERROR_MESSAGES ; $ validator = Validator :: make ( $ request -> all ( ) , $ rules , $ validattionErrorMessages ) ; if ( $ validator -> fails ( ) ) { return redirect ( ) -> route ( 'student.edit' , [ 'slug' => $ slug ] ) -> withErrors ( $ validator -> errors ( ) ) -> withInput ( ) ; } else { $ columnsInTable = \ Illuminate \ Support \ Facades \ Schema :: getColumnListing ( 'students' ) ; foreach ( $ request -> all ( ) as $ key => $ value ) { if ( in_array ( $ key , $ columnsInTable ) && $ key != 'photo' && $ key != 'description' ) { $ model -> $ key = $ value ; } } $ model -> slug = Str :: random ( 6 ) . '-' . Str :: slug ( $ model -> name ) ; if ( ! $ request -> has ( 'interest' ) ) { $ model -> interest = null ; } if ( $ request -> file ( 'photo' ) ) { Storage :: delete ( 'public/photos/' . $ model -> photo ) ; Storage :: delete ( 'public/photos/thumbnail/' . $ model -> photo ) ; $ path = Storage :: putFile ( 'public/photos' , $ request -> file ( 'photo' ) ) ; $ model -> photo = str_replace ( 'public/photos/' , '' , $ path ) ; $ image = $ request -> file ( 'photo' ) ; $ destinationPath = storage_path ( 'app/public/photos/thumbnail' ) ; $ img = Image :: make ( $ image -> getRealPath ( ) ) ; $ img -> resize ( 100 , 100 , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) -> save ( $ destinationPath . '/' . $ model -> photo ) ; } if ( $ request -> file ( 'description' ) ) { Storage :: delete ( 'public/descriptions/' . $ model -> description ) ; $ path = Storage :: putFile ( 'public/descriptions' , $ request -> file ( 'description' ) ) ; $ model -> description = str_replace ( 'public/descriptions/' , '' , $ path ) ; } 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 ( 'student' ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .
/**
         * if the item has been removed
         */ /**
         *
         */ /**
             * save on database
             */ //