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

Laravel中Service表无法存储数据,提交后重定向至创建页求助

问题诊断与解决方案

1. 字段拼写不一致(核心问题)

表单中Service D的标题字段name为titled,但控制器验证规则里写的是tittled,这会直接导致验证失败,触发重定向回创建页面,根本执行不到Service::create()步骤。

修复方法:

  • 方案一:统一视图字段名与验证规则
    将视图中Service D的输入框代码:
<input type="text" class="form-control" name="titled" placeholder="Title D">

修改为:

<input type="text" class="form-control" name="tittled" placeholder="Title D">

同时把对应的错误提示@error('titled')改为@error('tittled')。

  • 方案二:统一验证规则与视图字段名
    将控制器验证规则里的'tittled' => 'required',改为'titled' => 'required',,后续代码中所有涉及tittled的地方同步改为titled。

2. 模型批量赋值检查

确认Service模型已将所有需要存入的字段加入$fillable属性,否则create()方法会因批量赋值限制静默失败:

class Service extends Model
{
    protected $fillable = [
        'tittlea', 'descriptiona', 'imagea',
        'tittleb', 'descriptionb', 'imageb',
        'tittlec', 'descriptionc', 'imagec',
        'tittled', 'descriptiond', 'imaged', // 注意字段名需与最终统一的拼写一致
    ];
}

3. 图片存储路径权限验证

检查public/image/目录是否存在且具备写入权限,若不存在可手动创建:

mkdir -p public/image && chmod -R 755 public/image

也可在代码中自动创建目录:

$destinationPath = 'image/';
if (!file_exists(public_path($destinationPath))) {
    mkdir(public_path($destinationPath), 0755, true);
}

调试建议

若修复后仍有问题,可在Service::create()处添加异常捕获,查看具体错误信息:

try {
    Service::create($input);
} catch (\Exception $e) {
    dd($e->getMessage());
}

内容的提问来源于stack exchange,提问作者Moustapha taleb Jiddou

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:53:16