作者:幸运幸福摩天轮的世界 | 来源:互联网 | 2023-10-17 16:28
我尝试使用Vue.js在Laravel中实现image upload
吗?但是,我不知道为什么图像不保存到数据库中。我不确定,真正的问题出在我的Vue.js
或My Controller
内部。我还是,不知道怎么办。
CustomerController
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required','email' => 'required','phone' => 'required','company_name' => 'required',]);
$customer = Customer::create($data);
if ($request->hasFile('image')) {
$image = $request->get('image');
$imageName = time().'.'.$request->image->getclientOriginalExtension();
$request->image->move(public_path('images'),$imageName);
}
// dd($customer);
return response()->json(['sucessfully created image']);
}
我认为您忘记了保存图像
尝试
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required','email' => 'required','phone' => 'required','company_name' => 'required',]);
$customer = Customer::create($data);
if ($request->hasFile('image')) {
$image = $request->get('image');
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'),$imageName);
// Get the property name of table I suppose its imgData
$customer->imgData = $imageName;
$customer->save();
}
return response()->json(['sucessfully created image']);
}
或者如果您有多对多的关系
只需用attach方法替换该行
让我知道这是否对您有帮助