外键约束使用 save() 失败但在 Lumen 中使用 create() 没有失败

A foreign key constraint fails using save() but not failing with create() in Lumen

我正在尝试 post 一个对象,但是在使用 save() 函数时出现以下错误:

Cannot add or update a child row: a foreign key constraint fails

奇怪的是,如果我使用 create() 而不是 save(),它不会失败。你们能告诉我为什么会这样吗?

我就是这样做的:

$post = new Post();
$input = $request->all();
$post->save();

这是我的 Post 型号:

class Post extends Model{

    protected $fillable = [
        'creator_id', 'title'
    ];

    public function user(){
        return $this->belongsTo(User::class);
    }
}

这是用户模型:

class User extends Model{

    protected $fillable = [
        'first_name', 'last_name', 'email', 'profile_image_url'
    ];

    protected $hidden = [
        'password',
    ];
}

我有用户 table 和 Posts table。关系是 Posts table 中的 posts.creator_idusers.id.

我还想提一下,refular SQL 查询插入数据也很好。

据此,我希望您的输入表单中没有 array 这样的 <input name="fields[]">

你应该像$post->column_name = 'column_value'

那样做
$input = $request->all();
$post = new Post();

foreach( $input as $key => $value ){
    // Doing like $post->title = 'My Post Title'
    $post->$key = $value;
}

// If the creator_id is not in the form
if( empty( $input['creator_id'] ) ){
    $post->creator_id = $user_id; // Or use Auth::user()->getId();
}

$post->save();

正如我在您的模型中看到的那样,您没有问题。

如果你能用 dd($request->all());

告诉我你的 $request->all(); 里面有什么

之后,检查您的 $request->all() 是否有 users.id

并改变这个:

$post = new Post();
$post = $request->all();
$post->save();

如果这对你不起作用,试试这个:

我认为 save 方法不起作用,因为您的 $request->all() 中没有 users.id 参数,而创建新行需要 posts.creator_id。为此,你有两个解决方案:首先你可以使用 create 方法,或者你可以使用 save 方法,但你需要做这样的事情:

$post = new Post();
post->name_of_col_1 = $request->get('name_of_col_1');
$post->creator_id = $request->get('creator_id');

正如您在上面的问题评论中提到的,您在 User 模型中缺少 Eloquent 关系的定义。此外,值得一提的是,在您的模型中声明 class 属性 protected $table 是一个好习惯,以避免任何混淆并确保 Eloquent 将与 table 我们希望它能一起工作。

关于您的 UserPost classes,它们应该定义为:

class User extends Model{

    // protected $table = 'name_of_users_table';
    protected $fillable = [
        'first_name', 'last_name', 'email', 'profile_image_url'
    ];

    protected $hidden = [
        'password',
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

class Post extends Model{

    // protected $table = 'name_of_posts_table';
    protected $fillable = [
        'creator_id', 'title'
    ];

    public function user(){
        // You need to explicitly set the 'foreign_id' to 'creator_id'
        return $this->belongsTo(User::class, 'creator_id');
    }
}

此外,您需要确保 posts table.

中有一个 creator_id

如果您需要更多帮助,请查看Laravel's docs,并关注一对多关系部分。

希望对您有所帮助!

干杯!

编辑 1:

解释为什么 OP 的方法不起作用。

基本上,根据您最初的问题,您有:

$post = new Post();
$input = $request->all();
$post->save();

此时,您没有为 $post 对象分配任何内容,因此,无论何时执行 $post->save(),您的对象都是空的,因为它没有分配任何属性(除了那些 Eloquent 在引擎盖下分配给它的)。由于你有一个约束(Eloquent 关系),但你的对象是空的,你得到错误:

Cannot add or update a child row: a foreign key constraint fails

要摆脱它,您可以将代码修改为以下内容,它将起作用:

$post = new Post();
$input = $request->all();

// NOTE: Will only work if your provided 'creator_id' is set, as in is not null
$post->creator_id = 1; // Change later to: $input['creator_id']
$post->email = 'email@email.com' // Maybe set the email too

// Save the object (this time is not empty)
$post->save();

这样,您将保存满足 FK 约束的 Post 模型实例。

我希望这个解释能帮助你更好地理解!