簡體   English   中英

NULL 值保存在數據庫中而不是用戶提交的信息

[英]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());

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

我認為你應該使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM