如何使用laravel自定义请求和默认请求

时间:2017-11-23 06:02:38

标签: php laravel

我尝试为我的产品获取多个图片,并按照tutorial我的问题如果我使用教程,我会收到错误,因为从图片上传器控件保存产品$product = Product::create($request->all());但我有我自己对ProductController中的产品进行了验证,并且不想在ImageController

中使用此验证程序

所以我决定添加

foreach ($request->photos as $photo) {
  $filename = $photo->store('images');
  Image::create([
 'product_id' => $product->id,
 'filename' => $filename
 ]);
}
在我的ProductController $product->save();之后

获取产品ID并将$request->photos更改为UploadRequest::photos,这是:

foreach (UploadRequest::photos as $photo) {
          $filename = $photo->store('images');
          Image::create([
              'product_id' => $product->id,
              'name' => $filename
          ]);
      }

然后我在ProductController

的顶部添加了我的请求
use App\Http\Requests\UploadRequest;

我现在得到的错误是:

  

未定义的类常量'照片'

以下是我的完整代码:

ProductController存储方法

public function store(Request $request)
    {
      //Validating title and body field
      $this->validate($request, array(
          'title'=>'required|max:225',
          'slug' =>'required|max:255|unique:products',
          'sku' =>'nullable|max:255',
          'stock' =>'nullable|max:255',
          'user_id' =>'required|numeric',
          'short_description' => 'nullable|max:1000',
          'description' => 'required|max:100000',
          'subcategory_id' => 'required|numeric',
          'brand_id' => 'nullable|numeric',
          'price' => 'required|numeric',
          'status_id' => 'nullable|integer',
          'weight' => 'nullable|max:255',
          'length' => 'nullable|max:255',
          'height' => 'nullable|max:255',
          'width' => 'nullable|max:255',
          'arrivialin' => 'nullable|max:255',
          'meta_description' => 'nullable|max:255',
          'meta_keywords' => 'nullable|max:255',
          'publish' => 'required',
        ));

      $product = new Product;

      $product->title = $request->input('title');
      $product->slug = $request->input('slug');
      $product->sku = $request->input('sku');
      $product->stock = $request->input('stock');
      $product->user_id = $request->input('user_id');
      $product->description = $request->input('description');
      $product->short_description = $request->input('short_description');
      $product->subcategory_id = $request->input('subcategory_id');
      $product->brand_id = $request->input('brand_id');
      $product->status_id = $request->input('status_id');
      $product->price = $request->input('price');
      $product->weight = $request->input('weight');
      $product->length = $request->input('length');
      $product->height = $request->input('height');
      $product->width = $request->input('width');
      $product->arrivialin = $request->input('arrivialin');
      $product->meta_description = $request->input('meta_description');
      $product->meta_keywords = $request->input('meta_keywords');
      $product->publish = $request->input('publish');

      $product->save();

      $product->submores()->sync($request->submores, false);
      $product->suboptions()->sync($request->suboptions, false);


      foreach (UploadRequest::photos as $photo) {
          $filename = $photo->store('images');
          Image::create([
              'product_id' => $product->id,
              'name' => $filename
          ]);
      }



      //Display a successful message upon save
      Session::flash('flash_message', 'Product, '. $product->title.' created');
      return redirect()->route('products.index');
    }

上传请求

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UploadRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required'
        ];
        $photos = count($this->input('photos'));
        foreach(range(0, $photos) as $index) {
            $rules['photos.' . $index] = 'image|mimes:jpeg,bmp,png|max:2000';
        }

        return $rules;
    }
}

路由

Route::resource('/products', 'ProductController');
  Route::get('/upload', 'ImageController@uploadForm');
  Route::post('/upload', 'ProductController@store')->name('imageuploader');

上传表格

<form method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    Product name:
    <br />
    <input class="form-control" type="text" name="title" />
    <br /><br />
    Product photos (can attach more than one):
    <br />
    <input class="form-control" type="file" name="photos[]" multiple />
    <br /><br />
</form>

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

您的自定义UploadRequest应作为控制器方法中的参数传递。因此,您的商店方法应如下所示:

// ProductController.php
public function store(UploadRequest $request) {...}

然后,您应该将ProductController中的验证规则移至UploadRequest规则。如果您需要与ImageController不同的规则组合,则应创建另一个FormRequest类并将其作为参数传递给控制器​​方法。

希望有所帮助:)