laurentiu86stan's avatar

Adding new profile fields but not updating the database

Yet again running into problems with developing my platform :)

Now I am trying to add new fields to platform user profiles but after doing all tasks (I guess) right, the database is not getting populated with them.

What are the normal steps I have to do to add new fields? I added a new migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('username')->nullable();
            $table->longText('description')->nullable()->default('text');
            $table->text('siteweb')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('username');
            $table->dropColumn('description');
            $table->dropColumn('siteweb');
        });
    }
};

Then I edited the profile controller:

$request->validate([
            'username' => ['required', 'text', 'max:255'],
            'description' => ['required', 'text', 'max:255'],
            'siteweb' => ['required', 'url', 'max:255'],
            'avatar' => 'required|image',
        ]);

After that I edited the user model:

protected $fillable = [
        'name',
        'username',
        'email',
        'password',
        'avatar',
        'username',
        'descriprion',
        'siteweb',
    ];

Then edited my blade form:

<form method="post" action="{{ route('profile.update') }}" class="mt-6 space-y-6">
      @csrf
      @method('patch')

      <div>
          <x-input-label for="username" :value="__('Nume Utilizator')" />
          <x-text-input id="username" name="username" type="text" class="mt-1 block w-full" :value="old('username', $user->username)" autocomplete="username" />
          <x-input-error class="mt-2" :messages="$errors->get('username')" />
      </div>

      <div>
        <x-input-label for="description" :value="__('Descriere Personală')" />
        <x-text-input id="description" name="description" type="text" class="mt-1 block w-full" :value="old('description', $user->description)" autocomplete="description" />
        <x-input-error class="mt-2" :messages="$errors->get('description')" />
    </div>

    <div>
      <x-input-label for="siteweb" :value="__('Site Personal')" />
      <x-text-input id="siteweb" name="siteweb" type="url" class="mt-1 block w-full" :value="old('siteweb', $user->username)" autocomplete="siteweb" />
      <x-input-error class="mt-2" :messages="$errors->get('siteweb')" />
  </div>

      <div class="flex items-center gap-4">
          <x-primary-button>{{ __('Salvează') }}</x-primary-button>

          @if (session('status') === 'profile-updated')
              <p
                  x-data="{ show: true }"
                  x-show="show"
                  x-transition
                  x-init="setTimeout(() => show = false, 5000)"
                  class="text-sm text-gray-600"
              >{{ __('Setări salvate!') }}</p>
          @endif
      </div>
  </form>

Please tell me what I'm doing wrong...

0 likes
15 replies
jlrdw's avatar

Were all fields added to the database after migrating, what errors? Where are other fields like password?

laurentiu86stan's avatar

@jlrdw The other fields are from the Breeze scaffolding and I get no errors. And yes, all fields were added to the database.

jlrdw's avatar

Where and how are you adding the image, you need:

enctype="multipart/form-data"

And where are you inserting the data?

laurentiu86stan's avatar

@jlrdw the avatar image is not the problem and it is added on another blade and with enctype="multipart/form-data"

jlrdw's avatar

@laurentiu86stan try put instead of patch and make a put route, then it should work.

But I don't follow put or patch here as this doesn't seem to be API data.

Edit:

With the new migration, you are sure DB wasn't over written.

laurentiu86stan's avatar

@jlrdw I have another blade with the other fields with patch and it works...I don't know why this does not work...

jlrdw's avatar

@laurentiu86stan what / where is the code that's doing the insert? I see the validation above but no insert / save.

laurentiu86stan's avatar

@jlrdw is that suppose to go in a model or where, cause I can't find one for the other fields??

laurentiu86stan's avatar

@jlrdw in the profilecontroller I have this update:

public function update(ProfileUpdateRequest $request)
    {
        $request->user()->fill($request->validated());

        if ($request->user()->isDirty('email')) {
            $request->user()->email_verified_at = null;
        }

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

        return Redirect::route('profile.edit')->with('status', 'profile-updated');
    }

Is this where it suppose to go?

jlrdw's avatar

@laurentiu86stan I don't know all of your code, see what is happening in the network tab in the request and response.

Another suggestion would be just gather all this information at one time at registration.

Lara_Love's avatar

for update use like :

 public function update(Request $request, $id)

    {
        $data = $request->validate([
            'link' => 'required',
            'image' => ['nullable', 'image'],
        ]);
        unset($data['image']);
        if ($request->hasFile('image')) {
            $fileName = time() . '_' . $request->file('image')->getClientOriginalName();
            $filePath = public_path('image/advertise_Sidebar/');
            $request->file('image')->move($filePath, $fileName);
            $data['image'] = "$fileName";
        }
        $item = Adsside::findOrFail($id);
        $item->update($data);
        return redirect()->route('adsSidebar.index');

Please or to participate in this conversation.