php - 为什么有两个输入被发送到数据库而不是一个?
问题描述
我只是不明白这是怎么回事。当用户输入文本时,它的两个副本被发送到数据库而不是一个。我很确定我的代码是正确的,但不确定为什么它会在数据库中发送用户输入的重复副本。
我究竟做错了什么?
这是我create_posts_table
在迁移文件夹中的:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('body', 140);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
发布到数据库:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function create(Request $request, Post $post) {
// create post
$createdPost = $request->user()->posts()->create([
'body' => $request->body
]);
// return response
return response()->json($post->with('user')->find($createdPost->id));
}
}
解决方案
推荐阅读
- css - 使用Vue js在相对div内动态Css缩放绝对div
- javascript - 一切都是真的(中级 Javascript 算法)
- spring - 尤里卡、配置和管理仅在一项服务中
- kubernetes - k8s PodSecurityPolicy 的 Helm 模板
- python - 将文本转换为字段 python
- electron - 如何防止任务管理器关闭电子应用程序
- typescript - 初始化条带变量后如何更改条带区域设置
- python - 为什么 scipy.stats.gamma.pdf 返回一个 0 数组?
- python - Django 错误:'list_filter[2]' 的值引用了 'is_active',它不引用字段。?
- laravel - 将数据从一个表移动到另一个表时数组到字符串的转换错误