RYOK
RYOK

Reputation: 473

laravel field with no default value

i have a small problem here and it is that the image field doesn't have a default value as the complier said but actually i assigned a value for it i know that this error pops up when the field should have a value but the programmer didn't assign a value to it i know all of this but i already assigned a value to my field so what i am missing here

controller

protected function validator(array $data)

    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'image' => ['required' , 'image' , 'mimes:jpeg,png,jpg', 'max:2048'],
        ]);
    }
 protected function create(array $data)
    {

        $imageName = time().'.'.request()->image->getClientOriginalExtension();
        request()->image->move(public_path('images'), $imageName);

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'image' => $imageName,
            'password' => Hash::make($data['password']),
        ]);
    }

view

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
                        @csrf

                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

                                @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                            </div>
                        </div>

                            <label  class="col-md-4 col-form-label text-md-right" >Upload a picture</label>
                            <div class="custom-file mb-3 col-sm-6">
                            <input type="file" class="custom-file-input center" id="image" name="image">
                            <label class="custom-file-label" for="customFile"></label>
                            </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

the problem is mainly from the controller but i said that i put the view just in case you want to see it

small note : the image is uploading and it is stored at public folder just like the code said but its name doesn't save at the database also the field image is string at my database because i just want its name so i could view it

snap shot

the error message

Upvotes: 1

Views: 1900

Answers (2)

Sujeet
Sujeet

Reputation: 3810

Set a default image for your image field. Also make sure you have made it fillable, if not set it in protected $fillable in your model User.php

You will get this error when a user doesn't upload an image.

$table->string('image')->default('default.jpg');//You can also add .png

Now have a default image in your storage with name default.jpg
This way the image will have a default value if user doesn't' upload it.

Upvotes: 2

Lyndon
Lyndon

Reputation: 301

You need to add the default value or make that field null in the migration file, not the controller or view.

$table->string('image')->nullable();

or...

$table->string('image')->default('value');

Upvotes: 0

Related Questions