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
相关产品推荐
相关产品推荐

