如何使用Laravel 5创建POST服务URL并将数据保存到数据库?

时间:2015-08-03 10:59:36

标签: api laravel laravel-5

我希望使用POST从移动应用程序获取一些数据并将其保存在我的laravel 5应用程序数据库中。

myController的

public function postBookingDetails(Request $request)
{
    $FodMaps = $request->only('name','fructose');
    return $FodMaps;
}
Route::post('booking', 'FodMapController@postBookingDetails');

表格结构:

  • id(自动增量)

  • name varchar

  • fructose varchar

模型

protected $table = 'food_directory';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['name', 'lactose', 'fructose','polyols','fructan'];

1 个答案:

答案 0 :(得分:0)

public function postBookingDetails(Request $request)
{
    $FodMaps = $request->only('name','fructose');
    $FoodDirectory = new FoodDirectory;
    $FoodDirectory->name = $request->input('name');
    $FoodDirectory->fructose = $request->input('fructose');
    $FoodDirectory->save(); 

    return $FoodDirectory;
}

也会添加到您的模型中:

public $timestamps = false;

并且当然不要忘记添加到控制器的顶部:

use App\FoodDirectory;