php 数据库上的设置信息未更新,但密码仍会更新

rryofs0p  于 2022-11-28  发布在  PHP
关注(0)|答案(2)|浏览(131)

我试图使一个配置文件设置页面,人们可以chane名称,电子邮件和密码,只有密码得到更新,但其余的保持不变。我不明白为什么下面我有代码注意我使用微风
settings.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Settings') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <!-- Personal Details heading -->
                    <div class="mb-4">
                        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                            {{ __('Personal Details') }}
                        </h2>
                    </div>
                    <!-- Show id with prefix-->
                    <div class="mb-4">
                        <x-label for="id" :value="__('ID')" />
                        <x-input id="id" class="block mt-1 w-full" type="text" name="id" :value="Auth::user()->id + 243254" disabled />
                    </div>
                    <form method="POST" action="{{ route('settings.update')}}">
                        @method('PUT')
                        @csrf
                        <div class="grid grid-cols-2 gap-6">
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="name" :value="__('Name')" />
                                    <x-input id="name" class="block mt-1 w-full" type="text" name="name" value="{{ auth()->user()->name }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="email" :value="__('Email')" />
                                    <x-input id="email" class="block mt-1 w-full" type="email" name="email" value="{{ auth()->user()->email }}" autofocus />
                                </div>
                            </div>
                            <br />
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="new_password" :value="__('New password')" />
                                    <x-input id="new_password" class="block mt-1 w-full"
                                             type="password"
                                             name="password"
                                             autocomplete="new-password" />
                                </div>
                                <br />
                                <div>
                                    <x-label for="confirm_password" :value="__('Confirm password')" />
                                    <x-input id="confirm_password" class="block mt-1 w-full"
                                             type="password"
                                             name="password_confirmation"
                                             autocomplete="confirm-password" />
                                </div>
                            </div>
                        </div>
                        <div class="flex items-center justify-end mt-4">
                            <x-button class="ml-3">
                                {{ __('Update') }}
                            </x-button>
                        </div>
                    </form>
                    <!-- Address History heading -->
                    <div class="mt-4">
                        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                            {{ __('Address History') }}
                        </h2>
                    </div>
                    <br />
                    <form method="POST" action="#">
                        @csrf
                        <!-- Previous Addresses History Table using tr -->
                        <table>
                            <tr>
                                <th>Address</th>
                                <th>Postcode</th>
                                <th>City</th>
                                <th>Country</th>
                                <th>From</th>
                                <th>To</th>
                            </tr>
                        </table>

                        <br />
                        <!--Add heading to add new address-->
                        <h3 class="font-semibold text-xl text-gray-800 leading-tight">
                            {{ __('Add new address') }}
                        </h3>
                        <br />
                        <!-- New Address form-->
                        <div class="grid grid-cols-2 gap-6">
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="address" :value="__('Address')" />
                                    <x-input id="address" class="block mt-1 w-full" type="text" name="address" value="{{ auth()->user()->address }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="city" :value="__('City')" />
                                    <x-input id="city" class="block mt-1 w-full" type="text" name="city" value="{{ auth()->user()->city }}" autofocus />
                                </div>
                            </div>
                            <br />
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="state" :value="__('State')" />
                                    <x-input id="state" class="block mt-1 w-full" type="text" name="state" value="{{ auth()->user()->state }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="zip" :value="__('Zip')" />
                                    <x-input id="zip" class="block mt-1 w-full" type="text" name="zip" value="{{ auth()->user()->zip }}" autofocus />
                                </div>
                            </div><br />
                            <!-- add from to which day living in this address-->
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="from" :value="__('From')" />
                                    <x-input id="from" class="block mt-1 w-full" type="date" name="from" value="{{ auth()->user()->from }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="to" :value="__('To')" />
                                    <x-input id="to" class="block mt-1 w-full" type="date" name="to" value="{{ auth()->user()->to }}" autofocus />
                                </div>
                            </div>


                        <div class="flex items-center justify-end mt-4">
                            <x-button class="ml-3">
                                {{ __('Add Address') }}
                            </x-button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

SettingsController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateSettingsRequest;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
    public function update(UpdateSettingsRequest $request){
        auth()->user()->update($request->only('name', 'email'));
        if ($request->input('password')){
            auth()->user()->update([
                'password' => bcrypt($request->input('password'))
            ]);

        }
        return redirect()->route('settings')->with('message', 'Settings updated');
    }
}

web.php

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});



Route::group(['middleware' => 'auth'], function(){
    Route::get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');

    Route::view('settings', 'settings')->name('settings');
    Route::view('creditcards', 'creditcards')->name('creditcards');
    Route::view('loans', 'loans')->name('loans');
    Route::put('settings', [\App\Http\Controllers\SettingsController::class, 'update'])
        ->name('settings.update');



});



require __DIR__.'/auth.php';

有人知道为什么吗?
我试着做假设,去掉表单上的值,

olmpazwi

olmpazwi1#

只需先获取一个用户模型示例,然后更新它

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateSettingsRequest;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
    public function update(UpdateSettingsRequest $request){
        $user = User::findOrFail(auth()->user()->id);

        $user->update($request->only('name', 'email'));
        if ($request->input('password')){
            $user->update([
                'password' => bcrypt($request->input('password'))
            ]);

        }
        return redirect()->route('settings')->with('message', 'Settings updated');
    }
}

顺便说一句,你的代码不是很有效。你的数据库因为更新而命中了两次。
相反,您应该使用mutator https://laravel.com/docs/9.x/eloquent-mutators#defining-a-mutator和Mass Assignment https://laravel.com/docs/9.x/eloquent#mass-assignment-json-columns
说到这里,您的模型可能会显示如下内容:

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
  'name',
  'email',
  'password',
];

/**
* Interact with the user's password.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
protected function password(): Attribute
{
    return Attribute::make(
      get: fn ($value) => $value,
      set: fn ($value) => bcrypt($value),
    );
}

然后在你的控制器里就几行:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateSettingsRequest;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
    public function update(UpdateSettingsRequest $request){
        $user = User::findOrFail(auth()->user()->id);
        $user->update($request->all());

        return redirect()->route('settings')->with('message', 'Settings updated');
    }
}
lmyy7pcs

lmyy7pcs2#

检查您的用户模型,可能其他字段不在可填充属性中

相关问题