0

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');
patricus
  • 59,488
  • 15
  • 143
  • 145
Abdulla
  • 441
  • 1
  • 9
  • 21
  • 1
    Did you add the fields to the `$fillable` array on your `Place` model? – patricus Mar 17 '16 at 06:04
  • @patricus, I have edited the question. Please check. I have included the $fillable array there. – Abdulla Mar 17 '16 at 06:11
  • 1
    Are you sure you're getting the data? Can you show the output of a `dd($request->all())` before the create statement? – patricus Mar 17 '16 at 06:15
  • patricus when I console dd($request->all()), it outputs the following:
    array (size=0)
      empty
    
    – Abdulla Mar 17 '16 at 07:11
  • 1
    Your request is empty then so its not sending the data. How are you sending the request, `GET` / `POST` via ajax? Can you post the code that sends the data to the `savePost()` method. – DavidT Mar 17 '16 at 10:54
  • @DavidT , I have edited the question. Please check. I am sending the request to savePlace() from an angular js app. – Abdulla Mar 17 '16 at 12:02
  • @Abdulla unfortunately i dont know AngularJS. However it seems there are other Q/As that suggest adding `headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}` to your call. – DavidT Mar 17 '16 at 13:24
  • @DavidT I will check it. Anyway thanks for the answers. :) – Abdulla Mar 17 '16 at 17:03

1 Answers1

1

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('&')
})
Community
  • 1
  • 1
patricus
  • 59,488
  • 15
  • 143
  • 145
  • 1
    thanks man. That solved my problem. You have no idea how much time I have spent on that. Thank you very much. – Abdulla Mar 18 '16 at 09:47