我是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]');
你將字段添加到'$ fillable'陣列上你'Place'模式? – patricus
@patricus,我編輯了這個問題。請檢查。我在那裏包含了$ fillable數組。 – Abdulla
你確定你正在獲取數據嗎?你能在create語句之前顯示'dd($ request-> all())'的輸出嗎? – patricus