【问题标题】:Showing success message but not stored into database laravel显示成功消息但未存储到数据库 laravel
【发布时间】:2021-07-02 06:42:49
【问题描述】:

这是控制器的代码:

public function save_product(Request $request){
    $request->validate([
        'product_name' => 'required',
        'product_price' => 'required',
        'product_category' => 'required',
        'description' => 'nullable',
        'image1' => 'required',
        'image2' => 'nullable',
        'image3' => 'nullable',
    ], [
        'product_name.required' => 'Product name field required',
        'product_price.required' => 'Asking price field required',
        'product_category.required' => 'Product category field required',
        'image1.required' => 'You need a upload image-1 field',
    ]);

    if($request->hasFile('image1') || $request->hasFile('image2') || $request->hasFile('image3')){
        $file1 = $request->file('image1');
        $file2 = $request->file('image2');
        $file3 = $request->file('image3');
        $text1 = $file1->getClientOriginalExtension();
        $text2 = $file2->getClientOriginalExtension();
        $text3 = $file3->getClientOriginalExtension();
        $fileName1 = time().'.'.$text1;
        $fileName2 = time().'.'.$text2;
        $fileName3 = time().'.'.$text3;
        $file1->move('uploads/product_image', $fileName1);
        $file2->move('uploads/product_image', $fileName2);
        $file3->move('uploads/product_image', $fileName3);
        $product = Product::create([
            'image1'=>$fileName1,
            'image2'=>$fileName2,
            'image3'=>$fileName3,
            'product_category' => trim($request->input('product_category')),
            'product_name' => trim($request->input('product_name')),
            'product_price' => trim($request->input('product_price')),
            'description' => trim($request->input('description')),
        ]);
    }
    return redirect()->route('add_product')
        ->with('success','Successfully added Product!');
}

这是刀片模板的代码:

@extends('admin.layouts.master')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12">
            @include('pages.partials.flash_message')
        </div>
    </div>
</div>
<div class="card card-default">
    <div class="card-header card-header-border-bottom">
        <h2>Add Product</h2>
    </div>
    <div class="card-body">
        <form action="{{route('save_product')}}" method="POST">
            @csrf
            <div class="form-group">
                <label for="exampleFormControlSelect12">Select Category</label>
                <input type="text" class="form-control" placeholder="Enter Arrival Time" 
                 id="exampleFormControlSelect12" name="product_category" list="product_category" 
                 autocomplete="off">
                <datalist class="form-control" id="product_category" style="display: none" >
                    <option value="Women Clothes"></option>
                    <option value="Jewellery"></option>
                    <option value="Shoes"></option>
                    <option value="Sun Glass"></option>
                    <option value="Hair Band"></option>
                </datalist>
            </div>
            <div class="form-group">
                <label for="inputGroupFile02">Upload Product Image-1</label>
            </div>
            @if ($errors->has('image1'))
                <span class="text-danger" style="font-weight: bold">{{ $errors->first('image1') }} 
                </span>
            @endif
            <div class="input-group my-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
                </div>
                <div class="custom-file">
                    <input type="file" name="image1" class="custom-file-input" id="inputGroupFile01" 
                    aria-describedby="inputGroupFileAddon01">
                    <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
                </div>
            </div>
            <div class="form-group">
                <label for="inputGroupFile02">Upload Product Image-2</label>
                <small>*Optional</small>
            </div>
            @if ($errors->has('image2'))
                <span class="text-danger" style="font-weight: bold">{{ $errors->first('image2') }} 
            </span>
            @endif
            <div class="input-group my-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
                </div>
                <div class="custom-file">
                    <input type="file" name="image2" class="custom-file-input" id="inputGroupFile02" 
                     aria-describedby="inputGroupFileAddon01">
                    <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
                </div>
            </div>
            <div class="form-group">
                <label for="inputGroupFile02">Upload Product Image-3</label>
                <small>*Optional</small>
            </div>
            @if ($errors->has('image3'))
                <span class="text-danger" style="font-weight: bold">{{ $errors->first('image3') }} 
                </span>
            @endif
            <div class="input-group my-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
                </div>
                <div class="custom-file">
                    <input type="file" name="image3" class="custom-file-input" id="inputGroupFile03" 
                    aria-describedby="inputGroupFileAddon01">
                    <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
                </div>
            </div>
            <div class="form-group">
                <label for="exampleFormControlInput1">Product Name or Title</label>
                <input type="text" name="product_name" class="form-control" 
                id="exampleFormControlInput1" placeholder="Enter Product Name or Title">
                @if ($errors->has('product_name'))
                    <span class="text-danger" style="font-weight: bold">{{ $errors- 
                    >first('product_name') }}</span>
                @endif
            </div>
            <div class="form-group">
                <label for="exampleFormControlInput2">Asking Price</label>
                <input type="number" name="product_price" class="form-control" 
                  id="exampleFormControlInput2" placeholder="Enter Asking Price">
                @if ($errors->has('product_price'))
                    <span class="text-danger" style="font-weight: bold">{{ $errors- 
                     >first('product_price') }}</span>
                @endif
            </div>
            <div class="form-group">
                <label for="exampleFormControlTextarea1">Product Description</label>
                <textarea class="form-control" name="product_description" 
                id="exampleFormControlTextarea1" rows="3"></textarea>
            </div>
            <div class="form-footer pt-4 pt-5 mt-4 border-top">
                <button type="submit" class="btn btn-primary btn-default">Submit</button>
            </div>
        </form>
    </div>
</div>
@endsection

路线: Route::post('/admin/add_product','App\Http\Controllers\Admin\AdminController@save_product') ->name('save_product')->middleware('admin');

型号: 受保护的 $fillable = [ '产品名称', '产品分类', 'product_price', '描述', '图像 1', '图像2', '图像 3', ];

迁移:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('product_name');
        $table->string('product_category');
        $table->string('product_price');
        $table->text('description')->nullable();
        $table->string('image1');
        $table->string('image2')->nullable();
        $table->string('image3')->nullable();
        $table->timestamps();
    });
}

我在我的代码中找不到任何错误,请帮助我找出答案。提前致谢。

【问题讨论】:

  • 首先使用dd()检查您的条件是否有效
  • 为每张图片上传分别设置if条件。
  • 好的。让我试试
  • 使用 dd($request->all()) 检查您是否在验证后获取所有输入数据

标签: laravel


【解决方案1】:

您必须为每张图片分别设置 if 条件。试试这个:

        $fileName1='';
        $fileName2='';
        $fileName3='';
        if($request->hasFile('image1')) {
            $file1 = $request->file('image1');
            $text1 = $file1->getClientOriginalExtension();
            $fileName1 = time() . '.' . $text1;
            $file1->move('uploads/product_image', $fileName1);
        }
        if($request->hasFile('image2')) {
            $file2 = $request->file('image2');
            $text2 = $file2->getClientOriginalExtension();
            $fileName2 = time() . '.' . $text2;
            $file2->move('uploads/product_image', $fileName2);
        }
        if($request->hasFile('image3')) {
            $file3 = $request->file('image3');
            $text3 = $file3->getClientOriginalExtension();
            $fileName3 = time() . '.' . $text3;
            $file3->move('uploads/product_image', $fileName3);
        }
        
        $product = Product::create([
            'image1'=>$fileName1,
            'image2'=>$fileName2,
            'image3'=>$fileName3,
            'product_category' => trim($request->input('product_category')),
            'product_name' => trim($request->input('product_name')),
            'product_price' => trim($request->input('product_price')),
            'description' => trim($request->input('description')),
        ]);
        
        if($product){
        return redirect()->route('add_product')->with('success','Successfully added Product!');
        }
        else{
        return redirect()->back()->with('error','Error!');
        }

【讨论】:

  • 它工作但没有移动到 public/uploads/product_images 目录并且数据库中的图像文件名是空白的。
  • 如果条件仍然不进去。
  • 使用 dd($request->all()) 检查您是否在验证后获取所有输入数据
  • 确保在表单标签中使用 enctype="multipart/form-data"。
【解决方案2】:

在表单标签中使用 enctype="multipart/form-data"。

【讨论】:

    猜你喜欢
    • 2018-05-30
    • 2016-11-03
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多