[英]NULL value is saving in database instead of user submitted info
我是 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。
我的路线.php:
Route::get('/user/addnew', 'usercontroller@store');
我的用户控制器.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";
}
我的联系人 php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class contacts extends Model
{
protected $fillable =['firstname','lastname','phone'];
}
?>
现在,我的问题
当我 print_r $user 然后我收到错误,因为我的数组没有在控制台 window 中捕获我提交的数据 print_r ($user) output:
[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());
我认为你应该使用POST而不是GET。
$http.post("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);
});
和
Route::post('/user/addnew', 'usercontroller@store');
我注意到你发布到/index.php/user/addnew
你的路线是'/user/addnew'
'/index.php/user/addnew'
'/user/addnew'
'/index.php/user/addnew'
和'/user/addnew'
是不同的。 尝试发布到localhost:8000/user/addnew
解释:
/user/addnew
总是指host:port/user/addnew
而user/addnew
只会将它连接到您当前的URL
例如,如果您当前位于: localhost:8000/users/1
单击指向user/addnew
的链接将带您进入localhost:8000/users/1/user/addnew
而指向/user/addnew
的链接将带您进入localhost:8000/user/addnew
编辑:
这是多余的吗?
$scope.addnew.firstname=$scope.firstname;
$scope.addnew.lastname=$scope.lastname;
$scope.addnew.phone=$scope.phone;
你发送的是这个:
{"firstname": $scope.firstname, "phone": $scope.phone, "lastname": $scope.lastname}
我想你可以发送这个:
$scope.addnew
再次,你应该使用POST ,而不是GET 。
你有没有更新你的http请求发布? 你更新了网址吗?
实际上,我通过使用$request->input('firstname');
实现了这一点。 $request->input('firstname');
$request->input('firstname');
,因为,这就是 JSON 的原因。 否则,如果我一直在使用 Laravel 刀片文件,那么我可以通过上述解决方案来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.