Laravel 6存储数据问题,即使没有错误



我正在努力让用户能够发帖。我有一些错误,但现在我没有任何错误,但存储方法不起作用,即使它没有显示任何错误。这是home.balde.php 中的表单

<form method="POST" action="{{ url('form-store') }}">
@csrf
<img class="avatar" src="images/uploads/avatars/{{ $user->avatar }}" alt="pic" width="50px">
<label for="ftext">
<input type="text" id="text" name="text" placeholder="What's happening?" style="background-color: transparent; border-color: transparent; color: white;" required>
</label>
<br>
<div class="row">
<div class="col">
<label for="ftopic">
<input type="text" id="topic" name="topic" placeholder="Topic" style="background-color: transparent; border-color: transparent; color: white;" required>
</label>
</div>
<div class="col">
<label for="fhashtag">
<input type="text" id="hashtag" name="hashtag" placeholder="Hashtag" style="background-color: transparent; border-color: transparent; color: white;" required>
</label>
</div>
<div class="col">
<div class="text-end">
<button type="submit" class="tweetBtn">Tweet</button>
</div>
</div>
</div>
</form>

这是型号

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Tweets extends Model
{
protected $fillable = ['content', 'topic', 'hashtag'];
}

这是HomeController

public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$data = request()->validate([
'content' => 'required',
'topic' => 'required|email',
'hashtag' => 'required'
]);

$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}

以下是web.php 中的路线

Route::get('form', 'HomeController@index')->name('form');
Route::post('form-store', 'HomeController@store')->name('form-store');

当我点击提交时,页面将刷新,数据库中也不会发生任何事情

home.blade.php中没有<input name='content'>

由于在您的HomeController验证中,topic应为email。执行type=email,使其成为<input type="email" id="topic" name="topic" ...>

无论如何,这就是我在HomeController中的编码方式。也许你可以试试。

public function store(Request $request) {
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$validator = Validator::make($request->all(), [
'content' => 'required',
'topic' => 'required|email',
'hashtag' => 'required'
]);
if ($validator->fails()) {
abort(400, 'IDK Some Validation Error');
}
Tweets::create($request->all());
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}

检查此代码。。。

<label for="ftext">
<input type="text" id="text" name="text" placeholder="What's happening?" style="background-color: transparent; border-color: transparent; color: white;" required>
</label>

应该是name="content"而不是name="text"

相关内容

  • 没有找到相关文章

最新更新