简体   繁体   中英

Problem with updating data in database in Laravel

I have a form that has username, job, date of birth and city inputs. I am trying to update them through my form. When I click submit button my form submits but data remains unchanged. I successfully read them from database but when I try and update nothing happens. Any help is appreciated. Here is my code.

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>

rules

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']
    ];
}

i have a same problem in my code the error is:

i save like this: $user->save();

while i change the data using: $user and $user->profile

so i added in my code another line: $user->profile->save()

it work now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM