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