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

时间:2017-11-03 19:57:55

标签: php laravel lumen

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

  

无法添加或更新子行:外键约束失败

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

这就是我的所作所为:

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

这是我的帖子模型:

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',
    ];
}

我有用户表和帖子表。关系是在帖子表中,posts.creator_idusers.id

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

3 个答案:

答案 0 :(得分:1)

这样,我希望您输入表单中没有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();

答案 1 :(得分:1)

正如我在上面的模型中看到的,你没有问题。

如果您可以通过re

告诉我$request->all();内的内容

之后,检查您的dd($request->all());是否有$request->all()

并改变这一点:

users.id

如果这对您不起作用,请尝试以下方法:

我认为$post = new Post(); $post = $request->all(); $post->save(); 方法不起作用,因为您在save内没有users.id个参数,而创建新行需要$request->all() 。为此,您有两个解决方案:首先您可以使用posts.creator_id方法,或者您可以使用create方法执行此操作,但您需要执行以下操作:

save

答案 2 :(得分:1)

正如您在上面的问题评论中所提到的,您在User模型中错过了您的Eloquent关系的定义。此外,值得一提的是,在模型中声明类属性protected $table是一个好习惯,以避免任何混淆,并确保Eloquent可以使用我们希望它使用的表。

关于您的UserPost课程,应将其定义为:

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

此外,您需要确保creator_id表中有posts列。

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

我希望这有帮助!

干杯!

编辑1:

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

基本上,根据您的原始问题,您有:

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

此时,您没有为$post对象分配任何内容,因此,每当执行$post->save()时,您的对象为空,因为它没有分配任何属性(除此之外) Eloquent在引擎盖下分配的那些)。由于您有一个约束(Eloquent关系),但您的对象为空,您会收到错误:

  

无法添加或更新子行:外键约束失败

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

$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模型实例。

我希望这个解释可以帮助您更好地理解!