Проблема с обновлением данных в базе данных в Laravel - PullRequest
1 голос
/ 23 апреля 2020

У меня есть форма с именем пользователя, работой, датой рождения и городскими данными. Я пытаюсь обновить их через мою форму. Когда я нажимаю кнопку «Отправить», моя форма отправляется, но данные остаются без изменений. Я успешно прочитал их из базы данных, но при попытке обновления ничего не происходит. Любая помощь приветствуется. Вот мой код.

UserController. php

public function showProfile($username, Request $request)
{
    $profileId = User::getIdFromUsername($username);

    $userForShowProfile = User::with('userProfile')->where('id', $profileId)->first();

    return view('profile.show', compact('userForShowProfile'));
}

public function updatePersonalData(UpdatePersonalDataRequest $request)
{
    $user = Auth::user();

    $request->validated();

    $user->where('id', $user->id)->update(
        [
            'username' => $request->username,
            'job' => $request->job,
            'date_of_birth' => $request->date_of_birth,
            'updated_at' =>  Carbon::now()
        ]
    );

    $city = City::where('name', $request['city'])->first();

    if ($city != null && $city->count() > 0) {
        $request->user()->city()->associate($city->id);
    }

    $request->user()->save();

    return response()->json(null, 204);
}

web. php

Route::get('profile/{profile}', 'UserController@showProfile')->name('profile.show');
Route::patch('profile/personal', 'UserController@updatePersonalData')->name('profile.update.personal.data'); 

show.blade. php

<section data-edit="generalInfo" class="editGeneralInfo">
    <form action="{{ route('profile.update.personal.data') }}" method="POST" class="flex">
        @method('PATCH')
        @csrf
        <div class="form-group">
            <label for="" class="textBold">Name</label>
            <input type="text" name="username" value="{{ $userForShowProfile->username }}">
        </div>
        <div class="form-group">
            <label for="" class="textBold">Ort</label>
        <input type="text" name="job" value="{{ $userForShowProfile->job }}">
        </div>
        <div class="form-group">
            <label for="" class="textBold">Beruf</label>
            <input type="text" name="city" value="{{ $userForShowProfile->city->name }}">
        </div>
        <div class="form-group mb-0">
            <label for="" class="textBold">Geburtsdatum</label>
            <input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth }}">
            <p class="infoText mt-2">Dein Geburtsdatum wird nicht öffentlich angezeigt.</p>
            <p class="infoText">Wir ermitteln damit nur dein Alter</p>
        </div>
    <div class="form-group">
            <label for="" class="textBold">Button</label>
            <input type="submit" class="form-control" name="submit" value="BUTTON">
        </div>
    </form>
</section>

правила

public function rules()
{
    return [
        'username' => ['string', 'max:255', 'unique:users,username'],
        'job' => ['string', 'max:255'],
        'date_of_birth' => ['date', 'date_format:d.m.Y'],
        'city' => ['string', 'max:255', 'exists:cities,name']
    ];
}
...