【问题标题】:data not entered to database using Lumen (Laravel)未使用 Lumen (Laravel) 将数据输入数据库
【发布时间】:2016-07-03 08:09:27
【问题描述】:

我是 Laravel 和 Lumen 框架的新手。

我正在尝试使用 Lumen 框架创建 API。我想将数据输入数据库。但是数据库是用 id、date_created 和 date_updated 更新的。但是我输入的数据没有插入那里。相反,它对字符串输入显示空白,对整数输入显示 0。

这是我的控制器代码:

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

这是我的迁移代码:

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

我做得对吗?我应该使用任何其他代码吗?

请帮忙。

提前致谢。

编辑:

这是我的地方模型代码:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Place extends Model{

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

编辑 2:

我正在从 angularjs 应用程序(离子框架)发送请求。

这是我的 http.post 代码:

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

这是我的 routes.php 代码:

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

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

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

【问题讨论】:

  • 您是否将字段添加到 Place 模型上的 $fillable 数组中?
  • @patricus,我已经编辑了这个问题。请检查。我在那里包含了 $fillable 数组。
  • 您确定要获取数据吗?你能在创建语句之前显示dd($request-&gt;all()) 的输出吗?
  • patricus 当我控制台 dd($request->all()) 时,它输出以下内容:
     array (size=0)  
  • 您的请求是空的,所以它不发送数据。您如何通过 ajax 发送请求,GET / POST?您能否发布将数据发送到savePost() 方法的代码。

标签: php angularjs laravel lumen lumen-5.2


【解决方案1】:

根据this answer,问题似乎是角度发布数据的方式。基本上,它试图将数据作为 JSON POST,但 PHP 没有做任何事情来将 JSON 数据转换为请求查询数据。

要让它发挥作用,您需要做两件事。

首先,您需要将Content-Type 标头更改为application/x-www-form-urlencoded。但是,仅更改内容类型标头并不能解决问题,因为 Angular 仍在将数据作为 JSON 请求发布。

因此,第二,您需要将发布的数据从JSON格式更改为查询字符串格式(name=value&name=value)。

因此,将您的代码更新为如下内容:

$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('&')
})

【讨论】:

  • 谢谢伙计。这解决了我的问题。你不知道我为此花了多少时间。非常感谢。
猜你喜欢
  • 2021-08-14
  • 2019-08-27
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
相关资源
最近更新 更多