0

我是 Laravel 和 Lumen 框架的新手。

我正在尝试使用 Lumen 框架创建 API。我想将数据输入数据库。但是数据库是用 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','PlaceController@savePlace');
4

1 回答 1

1

根据这个答案,问题似乎是角度发布数据的方式。基本上,它试图将数据作为 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('&')
})
于 2016-03-17T18:40:19.957 回答