mysql - NULL 值保存在数据库中而不是用户提交的信息

标签 mysql angularjs laravel

我是 laravel 的新手,我面临以下问题

我的问题

我面临的问题是,每当我在填写名字姓氏和电话后提交表格时,除了我的 MYSQL 数据库数据中的一件事被保存为名字:NULL 姓氏:NULL 和电话:NULL 而不是保存我输入的数据。

我有一个 angularjs 表单,其中有名字姓氏和电话等字段。提交时它会转到 Controller 中的 submitContact():

提交.js:

 var addnew = angular.module('addnew',[]);

 // create angular controller
 addnew.controller('addContactController',            
 function($scope,$location,$window,$http) {


// function to submit the form after all validation has occurred            
$scope.submitContact = function() {

    $scope.addnew = {firstname:'', lastname:'',phone:''};

        $scope.addnew.firstname=$scope.firstname;
        $scope.addnew.lastname=$scope.lastname;
        $scope.addnew.phone=$scope.phone;


         $http.get("http://localhost:8000/index.php/user/addnew",{"firstname": $scope.firstname, "phone": $scope.phone, "lastname": $scope.lastname})
        .then(function mysuccess(response) {
            $scope.mycard = response.data;
            $scope.statuscode = response.status;
            $scope.statustext = response.statustext;
            $window.alert(JSON.stringify(response));
             console.log(response.data);
           });

};

});

http://localhost:8000/index.php/user/addnew这通过路由链接到我的 laravel。

我的 route.php:

 Route::get('/user/addnew', 'usercontroller@store');

我的用户 Controller .php

 public function store(Request $request)
{

  //contacts::create(Request::all());
  $user = new contacts;// contacts is my table name and my database is defined in .env file
  $user->firstname = Input::get('firstname');
  $user->lastname = Input::get('lastname');
  $user->phone = Input::get('phone');
  $user->save();
 //I Used print_r to see that weather my submitted data is coming in $user or not:
  print_r($user);

    echo "saved";
}

我的 contact.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class contacts extends Model
{
    protected $fillable =['firstname','lastname','phone'];
}

?>

现在,我的问题

当我打印_r $user 然后我收到错误,因为我的数组没有捕获我提交的数据 控制台窗口中的 print_r ($user) 输出:

[fillable:protected] => Array
    (
        [0] => firstname
        [1] => lastname
        [2] => phone
    )

[connection:protected] => 
[table:protected] => 
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
    (
        [firstname] => 
        [lastname] => 
        [phone] => 
        [updated_at] => 2016-10-07 03:46:34
        [created_at] => 2016-10-07 03:46:34
        [id] => 38
    )

[original:protected] => Array
    (
        [firstname] => 
        [lastname] => 
        [phone] => 
        [updated_at] => 2016-10-07 03:46:34
        [created_at] => 2016-10-07 03:46:34
        [id] => 38
    )

我想知道我在哪里犯了错误,我该如何纠正这个错误。

谢谢你的期待。

最佳答案

试试这个:

$user = new Contact();

取而代之的是:

$user = new contacts;

此外,将类名更改为 class Contact extends Model 并将文件名更改为 Contact.php

或者,由于您使用的是 $fillable,您可以创建新行:

Contact::create($request->all());

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

关于mysql - NULL 值保存在数据库中而不是用户提交的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39909384/

相关文章:

MySQL 语法错误 - END IF 行

c# - 从数据库中获取数据 C#

javascript - 为什么它总是在 AngularJS 中选择第一个 TD 单元格

eclipse - 如何在 Eclipse 中为 Laravel Blade 文件添加语法突出显示

javascript - Laravel Dusk 在表单确认后删除输入值

php - 如何访问与另一表相关的一列 Laravel 5.1

php - codeigniter 中 API 的更新查询不起作用

mysql - 使用 ROUND mysql 进行 UPDATE 语句

javascript - AngularJS - 如何对操作 HTML 的指令进行单元测试?

angularjs - Angular Dynamic Locale 无法立即工作