warpig's avatar
Level 12

'profile_pic' column returns NULL

Im trying to upload a picture/file but when it comes to reviewing the table, in SequelPro the column comes as null.

    <div>        
        <label 
            for="profile_pic">
        <span>
            Profile Picture
        </span>

        <input
            type="file"
            name="profile_pic"
            id="profile_pic"
            required
        >
    </div> <!-- profile picture end -->

The file does gets uploaded, I can see the file in the folder: public/storage/profiles, I have no problem with the request, in fact when I dd I get this:

array:6 [▼
  "full_name" => "Eduardo Robles Coello"
  "bio" => "About me..."
  "profile_pic" => "profiles/kgfaop7YiTGY5ztpaYUc8mTwTmlviFQ6aXZCRVBo.png"
  "linkedin_url" => "https://www.linkedin.com/in/hola-eduardo"
  "optional" => "Let go and let God"
  "user_id" => 1
]
    public function store(Profile $profile)
    {
        $profile = request()->validate([
            'full_name' => ['required'],
            'bio' => ['required'],
            'profile_pic' => ['required', 'image'],
            'linkedin_url' => ['url', 'nullable'],
            'optional' => ['nullable']
        ]);

        $profile['user_id'] = auth()->id();
        $profile['profile_pic'] = request()->file('profile_pic')->store('profiles');

        dd($profile);

        $profileObject = new Profile($profile);
        $profileObject->save();

        return redirect('/profile')->with('profile_created', 'Ay, new profile created!');
    }

And here's my migration:

        Schema::create('profiles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('full_name');
            $table->string('bio');
            $table->string('profile_pic')->nullable();
            $table->string('linkedin_url')->nullable();
            $table->string('optional')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });

I tried to remove the nullable() there for the column but got an error, what am I missing???

SQLSTATE[HY000]: General error: 1364 Field 'profile_pic' doesn't have a default value
0 likes
2 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

Is profile_pic included in fillable array?

1 like

Please or to participate in this conversation.