2016-03-17 45 views
0

我是Laravel和流明框架的新手。數據沒有輸入到數據庫使用流明(Laravel)

我想創建一個API使用流明framework.I想輸入數據到數據庫。但數據庫更新爲id,date_created和date_updated。但我輸入的數據沒有插入。相反,它顯示空白的字符串輸入和0整數輸入。

這裏是我的控制器代碼:

<?php 
namespace App\Http\Controllers; 

use App\Place; 
use App\User; 
use App\Http\Controllers\Controller; 
use Illuminate\Http\Request; 

class PlaceController extends Controller{ 

    public function savePlace(Request $request){ 
     $place = Place::create($request->all()); 
     return response()->json($place); 
    } 
} 

,這裏是我的遷移代碼:

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreatePlacesTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('places', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('place'); 
      $table->integer('pincode'); 
      $table->integer('bed'); 
      $table->integer('square_feet'); 
      $table->integer('price'); 
      $table->timestamps(); 
     }); 
    } 

    public function down() 
    { 
     Schema::drop('places'); 
    } 
} 

我這樣做是正確的嗎?我應該使用其他任何代碼嗎?

請幫忙。

在此先感謝。

編輯:

這裏是我的地方型號代碼:

<?php 
namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Place extends Model{ 

    protected $fillable = ['place', 'pincode', 'bed', 'square_feet', 'price']; 
} 

編輯2:

我把從angularjs應用程序(離子型框架)的請求。

這裏是我的http.post代碼:

app.controller('NavCtrl', ['$scope', '$http', '$location', '$window', function($scope,$http,$location,$window){ 
    $scope.data = {}; 
    $scope.savedata = function(){ 
    $http({ 
     url : "http://localhost/lumen/public/add", 
     method : "POST", 
     headers: $headers, 
     data : {'place':$scope.data.place,'pincode':$scope.data.pincode,'bed':$scope.data.bed,'square_feet':$scope.data.square_feet,'price':$scope.data.price} 
    }) 
    .success(function(data,status,headers,config){ 
     console.log(data); 
     $scope.navigat('/success.html'); 
    }) 
    .error(function(){ 
     alert("failed"); 
    }) 
    }; 

    $scope.navigat = function(url){ 
    $window.location.href=url; 
    }; 
}]); 

這裏是我的routes.php文件代碼:

<?php 
header("Access-Control-Allow-Origin: *"); 

$app->get('/', function() use ($app) { 
    return $app->version(); 
}); 

$app->post('lumen/public/add','[email protected]'); 
+1

你將字段添加到'$ fillable'陣列上你'Place'模式? – patricus

+0

@patricus,我編輯了這個問題。請檢查。我在那裏包含了$ fillable數組。 – Abdulla

+1

你確定你正在獲取數據嗎?你能在create語句之前顯示'dd($ request-> all())'的輸出嗎? – patricus

回答

1

this answer,這個問題似乎是這樣那角度是張貼數據。基本上,它試圖將數據作爲JSON發佈,但PHP並沒有做任何事情來將JSON數據轉換爲請求查詢數據。

要做到這一點,你需要做兩件事。

首先,您需要將Content-Type標題更改爲application/x-www-form-urlencoded。不過,只是更改內容類型標題並不能解決問題,因爲angular仍然將數據作爲JSON請求發佈。

因此,第二,您需要將從JSON格式發佈的數據更改爲查詢字符串格式(name = value & name = value)。

所以,更新您的代碼是這樣的:

$http({ 
    url : "http://localhost/lumen/public/add", 
    method : "POST", 
    headers: { "Content-Type" : "application/x-www-form-urlencoded" }, 
    data : [ 
     'place=' + encodeURIComponent($scope.data.place), 
     'pincode=' + encodeURIComponent($scope.data.pincode), 
     'bed=' + encodeURIComponent($scope.data.bed), 
     'square_feet=' + encodeURIComponent($scope.data.square_feet), 
     'price=' + encodeURIComponent($scope.data.price) 
    ].join('&') 
}) 
+1

感謝的人。這解決了我的問題。你不知道我花了多少時間。非常感謝你。 – Abdulla