首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未将所有值插入我的数据库(laravel-5.7)

未将所有值插入我的数据库(laravel-5.7)
EN

Stack Overflow用户
提问于 2018-12-28 15:52:20
回答 1查看 201关注 0票数 1

我试图通过Laravel中的表单将一个值插入到数据库中,但是我的all值没有插入。

只插入电子邮件、密码、created_date

这里是我的迁移代码:-

代码语言:javascript
复制
    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();
    });

这是我的控制器代码:-

代码语言:javascript
复制
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();

代码语言:javascript
复制
_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" 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-28 17:07:23

您可以使用create方法将新模型保存在一行中。插入的模型实例将从该方法返回给您。但是,在这样做之前,您需要在模型上指定一个fillableguarded属性,因为默认情况下,所有有说服力的模型都会防止大规模分配。

可填充,您可以指定哪些字段可以在模型中大量分配,可以通过向模型中添加特殊变量$fillable来实现。所以在模型中:

代码语言:javascript
复制
    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 (Link)中的含义。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53961071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档