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('&')
    })
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题