如何解决使用Laravel7.25版时无法存储在数据库中
我试图掌握Laravel框架并在从数据库的页面中检索值时陷入困境。
这些是我的控制器的片段,路线,视图部分,可能有助于了解我在做什么
web.php(用于路由):
<?php
use Illuminate\Support\Facades\Route;
Route::resource('posts','PostController');
Postcontroller.php(只是一个片段)
<?php
public function create()
{
//
return view('posts.create');
}
public function store(Request $request)
{
//
$this->validate($request,array(
'title' => 'required','body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show',$post->id);
}
create.blade.php(用于查看)
<div class="row">
<div class="col-mid-8 col-md-offset-2" >
<h1>Create New Post</h1>
<hr>
<form route='posts.store' >
@csrf
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
解决方法
代码的问题是您的输入没有name
属性,因此没有任何内容传递给后端代码。
像这样在输入中添加名称(它们必须有所不同,否则它们将被覆盖):
<input type="text" class="form-control" id="your-unique-input-id" name"your_unique_input_name">
然后,当您提交表单时,所有具有name
属性的输入将被传递到您的后端方法。然后,您可以使用之前指定的name
来获取它们的值。因此,在此示例中:
$request->your_unique_input_name // Which will return anything that user typed in that field
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。