Laravel:数组到字符串转换。发布表格

时间:2018-03-17 21:13:28

标签: laravel laravel-5 laravel-5.5

我正在尝试将表单发布到数据库,但是在提交

时出现错误
  

数组到字符串转换。我正在尝试上传多张照片,因此这是一个包含多个项目的数组。

有关如何修复此问题的任何建议。

这是mycontroller

公共功能商店(UploadRequest $ request){

      $Advert = PropertyAdvert::create($request->all());
      foreach ($request->photos as $photo) {
          $filename = $photo->store('photo');
          PropertyAdvert::create([
              'property_id' => $property->id,
              'filename'    => $filename,
              "address"     => $address,
              "county"      => $county,
              "town"        => $town,
              "type"        => $type,
              "rent"        => $rent,
              "date"        => $date,
              "bedrooms"    => $bedrooms,
              "bathrooms"   => $bathrooms,
              "furnished"   => $furnished,
              "description" => $description,
              "user_id" => Auth::id(),
          ]);
      }

    $id = $Advert->id;

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

照片表格字段

  <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>

上传请求字段

<?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()
    {
      $rules = [
        "photo" => "required|array",
      ];
      $photos = count($this->input('photos'));
      foreach(range(0, $photos) as $index) {
          $rules['photos.' . $index] = 'image|mimes:jpeg,bmp,png|max:2000';
      }

      return $rules;
    }
}

1 个答案:

答案 0 :(得分:-1)

使用

$request->file('photos') 

而不是

$request->input('photos')

请参阅此处的文档:https://laravel.com/docs/5.6/requests#retrieving-uploaded-files