php - 方法 Illuminate\Validation\Validator::validateImagetwo 不存在。我想添加两个图像
问题描述
我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。我想添加两个图像。我可以上传一张图片,但如果我尝试添加两张,则会显示错误。
<?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();
}
}
解决方案
您应该从验证中删除 imgTwo,因为它不存在,为什么要把它放在这里?告诉我原因,以便我提供解决方案
'imagetwo' => 'required|mimes:jpeg,png,jpg,gif,svg|max:512',
推荐阅读
- angular - 带有授权标头的 Angular 7 http(发布)-> 错误 500
- mercurial - 如何在 hgrc 中使用本地路径到硬盘上的存储库?
- sql - 为数据表分配 TVP 值
- java - java - 如何在java spring框架中绑定数组字段?
- javascript - 使用画布为ocr保存图像
- javascript - 如何让谷歌仪表从其上方的函数创建的输入标签中读取数据
- rest - Salesforce API - 使用复合字段。(无法从 VALUE_STRING 反序列化 MailingAddress 实例)
- java - 获取文件作为类路径上的资源
- spring - 如何在 Spring JPA 中访问嵌入式类的字段
- json - 使用 Codeable 难以从 JSON 中提取数据