我有一个未提交的Laravel表格?碰到了post方法,我可以在有效负载中看到数据,但不会通过表单提交。
当我通过邮递员对其进行测试时,它可以完美地工作。当我在浏览器中汇总时发生的是页面已刷新。如果我尝试返回提交的数据而不是发布数据,它仍然会刷新页面。任何建议
这是表格
<form method="POST" action="/blog/create">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleFormControlFile1">Cover Image</label>
<input type="file" name="image_name" class="form-control-file" id="exampleFormControlFile1">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Title</label>
<input type="text" class="form-control" name="title" id="title" placeholder="Title">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Tags</label>
<input type="text" class="form-control" name="tags" id="title" placeholder="Title">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Example textarea</label>
<textarea class="form-control" name="body" id="summernote" rows="3"></textarea>
</div>
<input type="submit" value="Submit">
</form>
这是提交方法
public function store(CreateBlogRequest $createBlogRequest, PhotoRequest $photoRequest)
{
$user = User::find(1);
// $user = auth()->user();
$post = $user->posts()->create($createBlogRequest->only(['title', 'body']));
$this->handleTags($post, $createBlogRequest->tags);
$this->handleImage($post, $photoRequest);
return $post;
}
public function handleTags(Post $post, $tags)
{
$tagsNames = explode(',', $tags);
$tags = [];
foreach($tagsNames as $tagName){
$tag = trim($tagName);
array_push($tags, $tag);
}
foreach($tags as $tagName){
Tag::firstOrCreate(['post_id' => $post->id, 'tag_name' => $tagName])->save();
}
}
public function handleImage(Post $post, $photoRequest)
{
$image_name = $photoRequest->file('image_name')->getRealPath();;
Cloudder::upload($image_name, null);
$result = Cloudder::getResult();
$photo = $post->photo()->create(['url' => $result['url'], 'public_id' => $result['public_id']]);
}
这些是路线
//后端帖子
Route::get('/dashboard', 'Backend\DashboardController@index')->name('dashboard.index');
Route::get('/blog/create', 'Backend\DashboardController@create')->name('post.create');
Route::post('/blog/create', 'PostController@store')->name('post.store');
知道为什么不提交吗?
答案 0 :(得分:0)
如果您无法从控制器返回请求,并且无一例外地返回了您,则很可能存在验证错误。尝试在视图中转储验证错误。
将其放在您的表单上方:
@if ($errors->any())
@foreach ($errors->all() as $error)
{{ $error }}
@endforeach
@endif
或尝试
{{ dd($errors->all() }}
答案 1 :(得分:0)
该表单还需要以下属性:enctype="multipart/form-data"
。它指定提交表单时要使用的内容类型。
没有此属性,文件上传将无法进行。
示例:<form method="POST" action="/blog/create" enctype="multipart/form-data">