drsh30452 2016-03-17 06:00
浏览 40
已采纳

数据未使用流明(Laravel)输入数据库

I am new to Laravel and Lumen framework.

I am trying to create an API using Lumen framework.I wanted to enter the data to database. But the database is updated with id, date_created and date_updated. But the data I entered are not inserted there. Instead it shows blank for string inputs and 0 for integer inputs.

Here is my controller code:

<?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);
    }
}

And here is my migration code:

<?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');
    }
}

Am I doing it correct? Should I use any other codes along with this?

Please help.

Thanks in advance.

EDIT :

And here is my place model code :

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Place extends Model{

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

EDIT 2 :

I am sending the request from an angularjs app(ionic framework).

Here is my http.post code :

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;
  };
}]); 

And here is my routes.php code :

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

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

$app->post('lumen/public/add','PlaceController@savePlace');
  • 写回答

1条回答 默认 最新

  • duanquanzhi5560 2016-03-17 18:40
    关注

    According to this answer, the issue seems to be the way that angular is POSTing the data. Basically, it is attempting to POST data as JSON, but PHP does not do anything to turn JSON data into request query data.

    To get this to work, you need to do two things.

    First, you need to change the Content-Type header to application/x-www-form-urlencoded. Just changing the content type header won't resolve the issue, though, because angular is still POSTing the data as a JSON request.

    Therefore, second, you need to change the data posted from the JSON format to the query string format (name=value&name=value).

    So, update your code to something like this:

    $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('&')
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配