Laravel始终设置默认值

时间:2018-07-17 10:55:52

标签: laravel

我正在尝试通过用户个人资料图片上传进行注册。(我被迫这样做) 我是这样创建迁移的:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nom');
        $table->string('prenom');
        $table->string('type')->default('visiteur');
        $table->boolean('confirme')->default(false);
        $table->string('email')->unique();
        $table->string('password');
        $table->string('photo_url')->default('default_photo_profile.jpg');
        $table->rememberToken();
        $table->timestamps();
    });

创建功能:

$request = request();


if ($request->hasFile('photo')) {
            $file = $request->file('photo');
            $fullname=$data['nom'].'_'.date("Y-m-d",time()).'.'.$file->getClientOriginalExtension();
            $path = $request->file('photo')->storeAs('images', $fullname);
        }

        return User::create([
            'nom' => $data['nom'],
            'prenom' => $data['prenom'],
            'email' => $data['email'],
            'photo_url' => $fullname,
            'password' => Hash::make($data['password']),
        ]);
  }

,文件字段的格式如下:

<div class="form-group">
    <label for="photo_url">Photo profile</label>
    <input type="file" name="photo" class="form-control-file" id="photo_url">
</div>

除photo_url字段外,其他所有东西都可以正常工作,它始终在迁移中设置默认值,而不是我在create函数中设置的值。 $ fullname已启动并已经声明。

整个表格:

<form method="POST" action="{{ route('register') }}" aria-label="{{ __('Register') }}"  enctype="multipart/form-data">
                    @csrf

                    <div class="form-group row">
                        <label for="nom" class="col-md-4 col-form-label text-md-right">Nom</label>

                        <div class="col-md-6">
                            <input id="nom" type="text" class="form-control{{ $errors->has('nom') ? ' is-invalid' : '' }}" name="nom" value="{{ old('nom') }}" required autofocus>

                            @if ($errors->has('nom'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('nom') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="prenom" class="col-md-4 col-form-label text-md-right">Prénom</label>

                        <div class="col-md-6">
                            <input id="prenom" type="text" class="form-control{{ $errors->has('prenom') ? ' is-invalid' : '' }}" name="prenom" value="{{ old('prenom') }}" required autofocus>

                            @if ($errors->has('prenom'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('prenom') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="email" class="col-md-4 col-form-label text-md-right">Email</label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

                            @if ($errors->has('email'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="password" class="col-md-4 col-form-label text-md-right">Mot de pass</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                            @if ($errors->has('password'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="password-confirm" class="col-md-4 col-form-label text-md-right">Mot de pass confirmation</label>

                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="photo_url">Photo profile</label>
                        <input type="file" name="photo" class="form-control-file" id="photo_url">
                    </div>
                    <div class="form-group row mb-0">
                        <div class="col-md-6 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                Envoyer
                            </button>
                        </div>
                    </div>
                </form>

出什么问题了?

3 个答案:

答案 0 :(得分:1)

假设您拥有$photo_url的值,请确保您的$fillables中有“ photo_url”。

当您有$ fillables时,它只会(通过User::create插入该数组中的内容,否则不会提交该变量。

您的$fillables应该如下所示:

$fillables = ['nom','prenom','type','confirme','email','password','photo_url'];

答案 1 :(得分:0)

用户模型中的'photo'数组中添加$fillable

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'photo_url', 
];

答案 2 :(得分:-2)

因此,根据您模型中的代码,您必须像下面一样添加photo_url in $fillable array

$fillables = ['nom','prenom','type','confirme','email','password','photo_url'];

好吧,现在有3种方法可以使用$ fillable来实现它,而您现在正在这样做。

第二种方式:

   if ($request->hasFile('photo')) { 
    $file = $request->file('photo'); 
    $fullname=$data['nom'].'_'.date("Y-m-d",time()).'.'.$file->getClientOriginalExtension(); 
    $path = $request->file('photo')->storeAs('images', $fullname); 
   } 
   else 
   { 
     $fullname = "default_photo_profile.jpg";  
   } 

return User::create([
            'nom' => $data['nom'],
            'prenom' => $data['prenom'],
            'email' => $data['email'],
            'photo_url' => $fullname,
            'password' => Hash::make($data['password']),
        ]);

在您的迁移中,将此$table->string('photo_url')->default('default_photo_profile.jpg');更改为$table->string('photo_url');

第三种方式:

$fullname = "default_photo_profile.jpg";

if ($request->hasFile('photo')) {
            $file = $request->file('photo');
            $fullname=$data['nom'].'_'.date("Y-m-d",time()).'.'.$file->getClientOriginalExtension();
            $path = $request->file('photo')->storeAs('images', $fullname);

 return User::create([
            'nom' => $data['nom'],
            'prenom' => $data['prenom'],
            'email' => $data['email'],
            'photo_url' => $fullname,
            'password' => Hash::make($data['password']),
        ]);
        }

        return User::create([
            'nom' => $data['nom'],
            'prenom' => $data['prenom'],
            'email' => $data['email'],
            'photo_url' => $fullname,
            'password' => Hash::make($data['password']),
        ]);
  }

好的,这些是实现的方法。我希望第一种和第二种方式的第三种方法比较冗长。

注意:对于第二种情况和第三种情况,您必须将迁移从此$table->string('photo_url')->default('default_photo_profile.jpg');更改为$table->string('photo_url'); 希望你能得到它。