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