没有默认值的laravel字段

时间:2019-06-23 22:58:00

标签: php laravel

我在这里有一个小问题,它是image字段没有编译器所说的默认值,但实际上我为其分配了一个值,我知道当该字段应该有一个值时会弹出此错误,但是程序员没有给它赋值,我知道所有这些,但是我已经给我的字段赋了值,所以我在这里错过了

控制器

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

查看

@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 error message

2 个答案:

答案 0 :(得分:2)

为图像字段设置默认图像。此外,请确保您已将其填充为可填充模型(如果未在模型protected $fillable

中的User.php中进行设置)
  

当用户不上传图片时,您会收到此错误。

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

现在您的存储中有一个默认图像,名称为 default.jpg
这样,如果用户不上传图片,该图片将具有默认值。

答案 1 :(得分:0)

您需要添加默认值或在迁移文件(而不是控制器或视图)中将该字段设置为空。

private async Task<RecognizerResult> RecognizeInternalAsync(ITurnContext turnContext, LuisPredictionOptions predictionOptions, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics, CancellationToken cancellationToken)
{
    var luisPredictionOptions = predictionOptions == null ? _options : MergeDefaultOptionsWithProvidedOptions(_options, predictionOptions);

    BotAssert.ContextNotNull(turnContext);

    if (turnContext.Activity.Type != ActivityTypes.Message)
    {
        return null;
    }

    // !! THIS IS THE IMPORTANT LINE !!
    var utterance = turnContext.Activity?.AsMessageActivity()?.Text;
    RecognizerResult recognizerResult;
    LuisResult luisResult = null;

    if (string.IsNullOrWhiteSpace(utterance))
    {
        recognizerResult = new RecognizerResult
        {
            Text = utterance,
            Intents = new Dictionary<string, IntentScore>() { { string.Empty, new IntentScore() { Score = 1.0 } } },
            Entities = new JObject(),
        };
    }
    else
    {
        luisResult = await _runtime.Prediction.ResolveAsync(
            _application.ApplicationId,
            utterance,
            timezoneOffset: luisPredictionOptions.TimezoneOffset,
            verbose: luisPredictionOptions.IncludeAllIntents,
            staging: luisPredictionOptions.Staging,
            spellCheck: luisPredictionOptions.SpellCheck,
            bingSpellCheckSubscriptionKey: luisPredictionOptions.BingSpellCheckSubscriptionKey,
            log: luisPredictionOptions.Log ?? true,
            cancellationToken: cancellationToken).ConfigureAwait(false);

        recognizerResult = new RecognizerResult
        {
            Text = utterance,
            AlteredText = luisResult.AlteredQuery,
            Intents = LuisUtil.GetIntents(luisResult),
            Entities = LuisUtil.ExtractEntitiesAndMetadata(luisResult.Entities, luisResult.CompositeEntities, luisPredictionOptions.IncludeInstanceData ?? true),
        };
        LuisUtil.AddProperties(luisResult, recognizerResult);
        if (_includeApiResults)
        {
            recognizerResult.Properties.Add("luisResult", luisResult);
        }
    }

    // Log telemetry code
    await OnRecognizerResultAsync(recognizerResult, turnContext, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);

    var traceInfo = JObject.FromObject(
        new
        {
            recognizerResult,
            luisModel = new
            {
                ModelID = _application.ApplicationId,
            },
            luisOptions = luisPredictionOptions,
            luisResult,
        });

    await turnContext.TraceActivityAsync("LuisRecognizer", traceInfo, LuisTraceType, LuisTraceLabel, cancellationToken).ConfigureAwait(false);
    return recognizerResult;
}

或...

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}