You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Backpack保存实体时user_id数据库存为null的排查求助

Troubleshooting: user_id remains null when saving new entity in Laravel Backpack

Let's walk through practical troubleshooting steps tailored to your scenario and code:

1. Verify if user_id is actually being added to the request

First, add a quick debug check right before calling traitStore() to confirm two critical things: that backpack_user()->id returns a valid user ID, and that your request actually contains the user_id after you attempt to add it.

public function store() { 
    // Debug to confirm user exists and user_id is added to request
    dd($this->crud->getRequest()->request->all(), backpack_user()?->id);
    
    $this->crud->getRequest()->request->add(['user_id' => backpack_user()->id]); 
    $response = $this->traitStore(); 
    return $response; 
}

If backpack_user() returns null, that means the current request isn't authenticated (even though validation passes, it’s possible validation runs before this code). If the user exists but user_id isn’t in the request array, there’s an issue with how you’re modifying the request instance.

2. Check if the trait methods use your modified request

Laravel Backpack’s traitStore() and traitUpdate() usually rely on $this->crud->getRequest(), but if the trait (or any custom overrides) uses the global request() helper instead, your changes won’t be picked up.

Peek at the trait’s source code. If it uses:

$request = request();

Instead of:

$request = $this->crud->getRequest();

Your added user_id won’t be included in the save operation. You may need to adjust the trait or ensure you’re modifying the correct request instance.

3. Double-check the model’s fillable/guarded properties

Even if you added user_id to $fillable, triple-check for typos (e.g., userid instead of user_id). Also, confirm your model doesn’t have a $guarded array that blocks mass assignment:

// This blocks ALL mass assignment, including user_id
protected $guarded = ['*'];

// Instead, use one of these:
protected $guarded = []; // Allow all fillable attributes
// OR explicitly exclude non-assignable fields
protected $guarded = ['id', 'created_at'];

If $guarded includes user_id, it will prevent the field from being set via mass assignment.

4. Look for model observers or global scopes interfering

Check if your model has a saving or creating observer that resets user_id to null. For example:

// In an observer class
public function saving(YourModel $model) {
    $model->user_id = null; // This overwrites your value
}

Temporarily disable any observers for your model and test the save again to rule out this possibility. Global scopes are less likely to affect writes, but it’s worth checking if any scope modifies the model before saving.

5. Confirm database field constraints

Even though validation requires user_id, check if the user_id column in your database is set to NOT NULL. If it allows null values, silent failures elsewhere could prevent the value from persisting. Also, ensure the column type matches your user ID type (e.g., unsignedBigInteger for typical Laravel user IDs).

6. Check if mass assignment is used in the trait

If traitStore() doesn’t use mass assignment (i.e., $model->create($request->all())) and instead sets attributes manually, your user_id won’t save unless explicitly set. For example:

// If the trait does this:
$model = new YourModel();
$model->name = $request->name;
$model->description = $request->description;
$model->save();

You’ll need to add $model->user_id = $request->user_id; before saving, or ensure the trait uses mass assignment with your fillable attributes.


内容的提问来源于stack exchange,提问作者Urudin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:07:46