照片值为空

时间:2018-03-17 23:22:03

标签: laravel laravel-5

我按照Laravel每日教程上传多张照片。除了我无法访问照片外,一切似乎都有效。当我在照片上使用dd时,我得到一个空值。

这是我的控制器。我将该属性存储在一个表中,将照片存储在另一个表中。这两个表在迁移中链接。

    public function store(Request $request, UploadRequest $request1){



    $Advert = PropertyAdvert::create([
        "address"     => $request->address,
        "county"      => $request->county,
        "town"        => $request->town,
        "type"        => $request->type,
        "rent"        => $request->rent,
        "date"        => $request->date,
        "bedrooms"    => $request->bedrooms,
        "bathrooms"   => $request->bathrooms,
        "furnished"   => $request->furnished,
        "description" => $request->description,
        "user_id" => Auth::id(),
    ]);

    dd($request1->photos);

    foreach ($request1->photos as $photo) {
            $filename = $photo->store('photo');
            PropertyAdvertPhoto::create([
                'property_id' => $Advert->id,
                'filename' => $filename
            ]);
          }

    $id = $Advert->id;

    return redirect("/property/$id");
  }

UploadRequest"请求文件"

这是存储所有验证规则的地方。     

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()
    {
      $rules = [
              'address' => 'required'
          ];
          $photos = count($this->input('photo'));
          foreach(range(0, $photos) as $index) {
              $rules['photo.' . $index] = 'min:100';
          }

          return $rules;

    }


}

上传表格

 <form method="POST" action="/property" enctype="multipart/form-data">
                        {{ csrf_field() }}

                          <div class="form-group row">
                              <label for="photo" class="col-md-3 col-form-label text-md-right">Images</label>
                              <div class="col-md-9">
                                <input required type="file" class="form-control" name="photo[]" multiple>
                              </div>
                            </div>

1 个答案:

答案 0 :(得分:0)

在您输入的表单中,您有名称照片[],并且在您的控制器中,您正试图使用​​dd($ request1-&gt;照片);,您必须使用dd($ request1-&gt; photo)< / p>

相关问题