【问题标题】:Method Illuminate\Validation\Validator::validateImagetwo does not exist. i want to add two image方法 Illuminate\Validation\Validator::validateImagetwo 不存在。我想添加两个图像
【发布时间】: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();

}
}

【问题讨论】:

  • 您的海量存储功能在哪里?您是否也在使用商店功能上传多张图片?你遇到了什么错误?请提供更多信息,不要只是复制粘贴同一个句子。
  • 是的,我使用了商店功能,我想一次上传两张图片,一张用于真实图像,另一张用于徽标。

标签: php laravel laravel-8


【解决方案1】:

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

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

【讨论】:

  • 我想在此处添加另一张图片,一张用于真实图片,另一张用于徽标
  • 这行你改了吗?
  • 不,我需要这一行或其他东西来添加更多图像
  • 您不必删除该行,但将其像答案一样,您只需删除imagetwo
  • 请将此标记为正确答案。谢谢
猜你喜欢
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 2020-02-08
  • 2021-11-22
  • 2022-11-04
  • 1970-01-01
  • 2016-09-12
相关资源
最近更新 更多