首页 > 解决方案 > 方法 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();

}
}

标签: phplaravellaravel-8

解决方案


您应该从验证中删除 imgTwo,因为它不存在,为什么要把它放在这里?告诉我原因,以便我提供解决方案

'imagetwo' => 'required|mimes:jpeg,png,jpg,gif,svg|max:512',

推荐阅读