Php Laravel保存不工作,数据库未更新
在我的主页上,我创建了一个评论表单,我正在将其提交到一个名为comments的表中。但是,一旦我提交表单,数据库就不会更新。 这是我的商店功能Php Laravel保存不工作,数据库未更新,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,在我的主页上,我创建了一个评论表单,我正在将其提交到一个名为comments的表中。但是,一旦我提交表单,数据库就不会更新。 这是我的商店功能 public function store(Request $request){ $validatedData = $request->validate([ 'name' => 'required', 'email' => 'required', 'phone_number' =&
public function store(Request $request){
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required',
'phone_number' => 'required',
'ask' => 'required'
]);
$comments = new Comment;
$comments->name = $request->input('name');
$comments->email = $request->input('email');
$comments->phone_number = $request->input('phone_number');
$comments->ask = $request->input('ask');
$comments->save();
return redirect('/welcome');
}
这是我的评论模型
Class Comment extends Model
{
protected $fillable =[
'name', 'email', 'phone_number', 'ask'
];
protected $primaryKey = 'id';
}
这是我的迁移
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('name');
$table->string('email');
$table->integer('phone_number');
$table->string('ask');
});
在应用商店功能中尝试以下操作:
公共函数存储(请求$Request){
$validatedData=$request->validate([
“名称”=>“必需”,
'电子邮件'=>'必需',
'电话号码'=>'必填',
'询问'=>'必需'
]);
注释::创建([
“名称”=>$request->name,
“电子邮件”=>$request->email,
“电话号码”=>$request->phone\u number,
“询问”=>$request->ask,
]);
返回重定向('/welcome');
}
[已编辑]
迁移:
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('name');
$table->string('email');
$table->string('phone_number');
$table->string('ask');
});
但我认为你在验证上有错误。试着解决这个问题。现在是我在表单中添加代码的时候了
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if($errors->any())
@foreach($errors->all()作为$error)
- {{$error}}
@endforeach
@恩迪夫
什么是$commets->id
?我试图保存id,因为没有保存,发生了什么?有错误吗?是否发生了重定向到“欢迎”的情况?我认为您在验证中有错误。对于此测试,请在store函数中的validate方法之后返回一些内容,并检查returns true?例如:return('hi')它没有显示错误,它正在重定向到welcome。因为它正在更新我的数据库,所以我想我应该保存id,但它没有再次工作。仍然一样,它没有更新我的数据库table@MamadouBellaDiallo所以你们的问题在于验证方法。验证方法之前使用:return$request->all()
并检查从您的formI中发布的内容。我是否看到输入的数据。在您的视图中,尝试使用$errors->any()
显示错误。要查看完整代码,请访问:@MamadouBellaDiallo查看我编辑的答案…我认为您的电话号码列类型错误