我写了一个发布请求,当我从邮递员发布数据时,一切正常,但文件未成功发布:
这是我写的方法:
public function create(Request $request)
{
try{
$validation = Validator::make($request->all(), [
'brand_id' => 'required',
'title' => 'required|max:255',
'description' => 'required|max:512',
'colors' => 'required',
'size' => 'required',
'price' => 'required',
'file' => 'required|file|mimes:jpeg,png,jpg,gif,svg,xlsx,xls,doc,docx,ppt,pptx,txt,pdf|max:1000048',
]);
if ($validation->fails()) {
$errors = $validation->errors();
return $this->respondWithError($errors);
} else {
$product = new products();
$product->user_id = $this->user_id;
$product->brand_id = $request->input('brand_id');
$product->title = $request->input('title');
$product->description = $request->input('description');
$product->colors = $request->input('colors');
$product->size = $request->input('size');
$product->price = $request->input('price');
$doc = $request->document;
$table_name = 'test';
$record_id = 1;
return $this->respondWithSuccess($request);
$path = $this->dms->storeFile($doc, $table_name, $record_id);
$product->image_urls = $path;
$product->save();
return $this->respondWithSuccess($product);
}
} catch (Exception $ex) {
return $this->respondWithError($ex->getResponse());
}
}
这是完整的方法。
这是我创建的用于访问此方法的路由:
Route::post('products/create', 'ProductsController@create');
此路由是API路由。 最后我打的邮递员电话是这样的: 头: 内容类型:应用程序/ x-www-form-urlencoded 接受:application / json
在主体中,我传递了所有数据和一个文件 我返回了请求对象,它就这样返回:
{
"message": "",
"status": "success",
"response": {
"brand_id": "1",
"title": "test",
"description": "test description",
"colors": "red",
"size": "25",
"price": "30",
"file": {}
}
}