【问题标题】:all value is not inserted in my database (laravel-5.7)所有值都没有插入我的数据库(laravel-5.7)
【发布时间】:2019-05-26 10:17:50
【问题描述】:

我尝试通过 Laravel 中的表单向数据库中插入一个值,但我的所有值都没有插入。

仅插入电子邮件、密码、创建日期

这是我的迁移代码:-

    Schema::defaultStringLength(191);
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('fname');
        $table->string('lname');
        $table->string('email')->unique();
        $table->string('phone');
        $table->string('gender');
        $table->string('dob');
        $table->string('religion');
        $table->string('mtn');
        $table->string('country');
        $table->string('city');
        $table->string('district');
        $table->string('upozila');
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

这是我的控制器代码:-

public function register(Request $request)
{
    $this->validation($request);
    User::create($request->all());
    return redirect('/');
}

public function validation($request)
{
    $validatedData = $request->validate([
    'fname' => 'required|max:255',
    'lname' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users,email',
    'phone' => 'required|max:255',
    'gender' => 'required|max:255',
    'dob' => 'required|max:255',
    'religion' => 'required|max:255',
    'mtn' => 'required|max:255',
    'country' => 'required|max:255',
    'city' => 'required|max:255',
    'district' => 'required|max:255',
    'upozila' => 'required|max:255',
    'password' => 'required|min:6',
    'confirm_password' =>'required|min:6|same:password',
     ]);
}

这是我的数组 = $request->all();

_token     "wDRoDeLkOFX5re5nba2Ufv5pr0iKzYVCr0tK9EFE"
fname      "nirab" 
lname      "nirax" 
email      "is@gmail.com" 
phone      "988907" 
gender     "male" 
dob        "2018-12-03" 
religion   "male" 
mtn        "male" 
country    "male" 
city       "male" 
district   "male" 
upozila    "male"
password   "1234567" 
confirm_password    "1234567" 

【问题讨论】:

    标签: laravel laravel-5 laravel-5.7


    【解决方案1】:

    是Laravel的一种保护机制, 在这里阅读更多内容:

    https://laravel.com/docs/5.7/eloquent#mass-assignment

    当您使用 create 时,请确保您的模型具有“$fillable”属性,如下所示:

    protected $fillable = [
        'fname',
        'lname',
        'email',
        'phone',
        'gender',
        'dob',
        'religion',
        'mtn',
        'country',
        'city',
        'district',
        'upozila',
        'password'
    ];
    

    另外,请注意您的密码在存储到数据库之前应该经过哈希处理。

    【讨论】:

    • 你也可以做受保护的 $guarded[] 并且你放入数组中的任何东西都不能批量填充。
    • 他还必须从请求中取消设置_token
    【解决方案2】:

    您可以使用create 方法将新模型保存在一行中。插入的模型实例将从该方法返回给您。但是,在这样做之前,您需要在模型上指定 fillableguarded 属性,因为所有 Eloquent 模型都默认防止批量赋值。

    Fillable 您可以指定模型中可以批量分配的字段,您可以通过向模型添加特殊变量 $fillable 来实现。所以在模型中:

        class users extends Model {
              protected $fillable = ['fname', 'lname', 'email', 'phone', 'gender', 'dob', 'religion', 'mtn', 'country', 'city', 'district', 'upozila', 'password']; 
              //only the field names inside the array can be mass-assign
        }
    

    更多细节:你可以阅读我的答案,在这里你可以很容易地理解 Laravel 中的“Mass Assignment”是什么意思 (Link)

    【讨论】:

    • 谢谢 ...但我在 app/user.php 中编辑受保护的 $fillable ..现在它正在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多