问题描述
我是Laravel的新手,我为页面制作了一个表单,用户可以添加新图像,该表单位于create.blade.PHP
中:
<form action="/p" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row">
<h1>Add New Post</h1>
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Post Caption</label>
<input id="caption"
type="text"
class="form-control @error('caption') is-invalid @enderror"
name="caption"
value="{{ old('caption') }}"
autocomplete="caption" autofocus>
@error('caption')
<span class="invalid-Feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@error('image')
<span class="invalid-Feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="row pt-4">
<button class="btn btn-primary">Add New Post</button>
</div>
</div>
</div>
Route::get('/p/create','PostsController@create');
Route::get('/p','PostsController@store');
Route::get('/profile/{user}','ProfilesController@index')->name('profile.show');
如您所见,它指的是PostController.PHP
:
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store()
{
dd(request()->all());
}
}
我也执行命令PHP artisan route:list
,就是这样:
那么这里出了什么问题?我进行了很多搜索,但找不到任何有用的信息。因此,如果您知道如何解决此问题,请告诉我。
预先感谢
解决方法
您正在将请求发送到服务器,因此需要将HTTP请求设置为post not get, 像这样
Route::post('/p','PostsController@store');
,
您需要添加一条POST
路线
Route::post('/p','PostsController@store');
,
-
在create.blade.php中,表单方法是
POST
,在web.php中是Route::get('/p','PostsController@store');
,所以您应该更改Route::post('/p','PostsController@store')
代替Route::get('/p','PostsController@store')
-
在控制器中
use Illuminate\Http\Request; class PostsController extends Controller { public function create() { return view('posts.create'); } public function store(Request $request) { dd($request->input('image')); } }