2016-03-17 06:00
浏览 40


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:

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:


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

class CreatePlacesTable extends Migration
    public function up()
        Schema::create('places', function (Blueprint $table) {

    public function down()

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

Please help.

Thanks in advance.


And here is my place model code :

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(){
      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}

  $scope.navigat = function(url){

And here is my routes.php code :

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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:

        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)
    打赏 评论

相关推荐 更多相似问题