创建方法无法正常工作。始终返回OK状态,数据为空且db中无插入。不幸的是没有错误显示,所以我不知道该怎么办。
protected function addBooking(Request $request)
{
$data = $request->all();
if ($this->validator($data)->fails()) {
return $this->sendError('Validation Error.', $this->validator($data)->errors());
}
Booking::create($data);
return $data;
}
这是迁移
public function up()
{
Schema::create('bookings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('booker_id')->nullable(false)->unsigned();
$table->bigInteger('classroom_id')->nullable(false)->unsigned();
$table->string('name');
$table->string('color')->default("#ff0000");
$table->string('file')->default(NULL);
$table->string('start')->nullable(false);
$table->string('end')->nullable(false);
$table->timestamps();
$table->foreign('booker_id')->references('id')->on('users');
$table->foreign('classroom_id')->references('id')->on('classrooms');
});
}
模型
class Booking extends Model
{
protected $fillable = [
'booker_id', 'classroom_id', 'name', 'color', 'file', 'start', 'end'
];
protected $hidden = [
];
protected $casts = [
];
}
我如何发送请求
{
"booker_id": 10,
"classroom_id": 4,
"name": "Microsoft",
"start": "2019-04-25 14:45",
"end": "2019-04-25 16:45",
"color": "#ff0000",
"file": "test"
}
答案 0 :(得分:0)
//您应该使用表单请求来验证数据。并移动所有业务逻辑进行建模
protected function addBooking(Request $request)
{
$data = $request->all();
if ($this->validator($data)->fails()) {
return $this->sendError('Validation Error.', $this->validator($data)->errors());
}
return Booking::create($data)->fresh();
}
答案 1 :(得分:0)
Laravel具有路由约定,如果要创建新项目,则方法名称应为store()。 我正在使用两种创建新元素的方式: 首先这要短一些,我在StoreBooking中添加验证
workflowPath && workflowPath.length &&
Object.keys(this.props.row._original).forEach((key) => {
...
基于laravel文档的记录器:
public function store( StoreBooking $request ) {
$data = $request->all();
$booking = Booking::query()->create( $data );
}