【发布时间】:2021-07-18 21:02:37
【问题描述】:
我想添加两张图片。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\Admin\StoreTagsRequest;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//$tags = Product::all();
//return view('products.index', compact('tags'));
$products = Product::latest()->paginate(5);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//$tag = Product::create($request->all());
//return redirect()->route('admin.tags.index');
$request->validate([
'name' => 'required',
'detail' => 'required',
'color' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'imagetwo' => 'required|imagetwo|mimes:jpeg,png,jpg,gif,svg|max:512',
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}
if ($imagetwo = $request->file('imagetwo')) {
$destinationPath = 'image/';
$profileImagetwo = date('YmdHis') . "." . $imagetwo->getClientOriginalExtension();
$imagetwo->move($destinationPath, $profileImagetwo);
$input['image'] = "$profileImagetwo";
}
Product::create($input);
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return view('products.show',compact('product'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
'color' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}
if ($imagetwo = $request->file('imagetwo')) {
$destinationPath = 'imagetwo/';
$profileTmagetwo = date('YmdHis') . "." . $imagetwo->getClientOriginalExtension();
$image->move($destinationPath, $profileTmagetwo);
$input['imagetwo'] = "$profileTmagetwo";
}else{
unset($input['imagetwo']);
}
$product->update($input);
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
function indextwo(){
//return DB::select("select * from products");
//DB::table('products')->orderBy('id','desc')->first();
return Product::orderBy('id', 'DESC')->first();
}
}
【问题讨论】:
-
您的海量存储功能在哪里?您是否也在使用商店功能上传多张图片?你遇到了什么错误?请提供更多信息,不要只是复制粘贴同一个句子。
-
是的,我使用了商店功能,我想一次上传两张图片,一张用于真实图像,另一张用于徽标。