我是laravel的新手,我正面临以下问题
我的问题
我面临的问题是,每当我在填写firstname lastname和phone之后提交表单,那么一切顺利,除了我的MYSQL数据库中的一个数据保存为firstname:NULL lastname:NULL和phone:NULL而不是保存我输入的数据。
我有一个angularjs表单,其中有firstname lastname和phone.on提交它转到控制器中的submitContact():
submit.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');
我的usercontroller.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'];
}
?>
现在,我的问题
当我print_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
)
我想知道我在哪里弄错了,我怎么能纠正这个错误。
在期待中感谢你。
答案 0 :(得分:0)
试试这个:
$user = new Contact();
而不是:
$user = new contacts;
另外,将班级名称更改为class Contact extends Model
,将文件名更改为Contact.php
。
或者,由于您使用$fillable
,因此可以创建新行:
Contact::create($request->all());
答案 1 :(得分:0)
我认为你应该使用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'
不同。
尝试发布到localhost:8000/user/addnew
而不是
解释:
/user/addnew
始终引用host:port/user/addnew
虽然user/addnew
会将其连接到您当前的网址
例如,如果您目前位于: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请求以发布?你更新了网址吗?