从输入上载文件到数据库

时间:2017-10-03 18:19:47

标签: php laravel-5

我正在学习Laravel并且遇到了一个问题。我已经创建了带验证的表单,并且都进入了数据库。我的image_urltype="file"

不同
  <form method="POST" action="{{route('photos.store')}}" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="">
   @component('components.back', ['url' => route('photos.index')])
   @endcomponent
   <hr>
</div>
<div class="form-all">
  <ul class="form-section page-section">
    <li class="form-line" data-type="control_image" id="id_1">
      <div id="cid_1" class="form-input-wide">
        <div style="text-align:center;">
          <img alt="" class="form-image" style="border:0px;" src="" height="42px" width="42px" data-component="image">
        </div>
      </div>
    </li>
    <li class="form-line" data-type="control_text" id="id_12">
      <div id="cid_12" class="form-input-wide">
        <div id="text_12" class="form-html" data-component="text">
          <p><span style="color:#363636;font-size:xx-large;">Įkelti nuotrauką</span></p>
        </div>
      </div>
    </li>

    <li class="form-group{{ $errors->has('title')?' has-error' : ''}}" id="id_7">
      <label class="form-label form-label-top" id="label_7" for="title">Pavadinimas</label>
      <div id="cid_7" class="form-input-wide">
        <input id="title" type="text" name="title" value="{{ old('title') }}" class="form-control" placeholder="Pavadinimas">
        @if ($errors->has('title'))
        <span class="help-block">
          <strong>
          {{ $errors->first('title')}}
          </strong>
        </span>
        @endif  </div>
    </li>

    <li class="form-group{{ $errors->has('description')?
      ' has-error' : ''}}">
      <label class="form-label form-label-top" id="label_5" for="description">Aprašymas</label>
      <div id="cid_5" class="form-input-wide">
        <input id="description" type="text" name="description" value="{{ old('description') }}" class="form-control" placeholder = "Aprašymas">
        @if ($errors->has('description'))
        <span class="help-block">
          <strong>
          {{ $errors->first('description')}}
          </strong>
        </span>
        @endif
      </div>
    </li>

    <li class="form-group {{ $errors->has('image_url')?
      ' has-error' : ''}}">
      <label class="form-label form-label-top" id="label_11" for="image_url">Nuotrauka</label>
      <div id="cid_11" class="form-input-wide">

        <input id="image_url" type="file" name="image_url" value="{{ old('image_url') }}" class="form-control">

        @if ($errors->has('image_url'))
        <span class="help-block">
          <strong>
          {{ $errors->first('image_url')}}
          </strong>
        </span>
        @endif
      </div>
    </li>

    <li class="form-group {{ $errors->has('category_id') ? 'has-error': ''}}">
      <label class="form-label form-label-top" id="label_11">Kategorija</label>
      @if(isset($photo))
      <select name="category_id" class="form-control">
        <option value="">Pasirinkti kategoriją</option>
        @foreach($categories as $category)
          <option value="{{$category->id}}">{{$category->title}}</option>
        @endforeach
      </select>
      @else
      <select name="category_id" class="form-control">
        <option value="">Pasirinkti kategoriją</option>
        @foreach($categories as $category)
          <option value="{{$category->id}}">{{$category->title}}</option>
        @endforeach
      </select>
      @endif

      @if($errors->has('category_id'))
      <span class="help-block">
        <strong>{{ $errors->first('category_id')}}</strong>
      </span>
      @endif

    </li>

    <li class="form-line" data-type="control_button" id="id_2">
      <div id="cid_2" class="form-input-wide">
        <div style="text-align:center;" class="form-buttons-wrapper">
          <button id="input_2" type="submit" class="form-submit-button" data-component="button">
            Patvirtinti
          </button>
        </div>
      </div>
    </li>

  </ul>
</div>

我的控制器看起来像这样:

public function create()
{
  $categories = Category::all();
  return view('photo.create', compact('photo','categories'));
}
/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate($request, [
   'title' => 'required|min:3',
   'image_url'=> 'required',
   'category_id' => 'required',
    ]);

     $photo = new Photo;


     $photo->title = $request->input('title');
     $photo->image_url = $request->input('image_url');
     $photo->description = $request->input('description');
     $photo->category_id = $request->input('category_id');
     $photo->save();

     $request->session()->flash('create', $request->title. "Sekmingai sukurta");
     $path = $request->image_url->store('images');
     return redirect()->route('photos.index');

}

和播种机:

public function __construct( Photo $photo, Category $category) {
$this->photo = $photo;
$this->category = $category;
}

public function run()
{
  $categories_ids = $this->category->pluck('id');

      DB::table('photos')->insert(
        [
          'title' => '',
          'image_url' => '',
          'description' => '',
          'category_id' => $categories_ids->random()
        ]);

}

我明白我应该把一些if放到Controller中告诉&#34;如果它不是文本,而是文件接受文件&#34;。但我无法在网上找到答案。 感谢您提前建议。

1 个答案:

答案 0 :(得分:0)

您将从$fileName = $request->image_url->store('images');获取图片名称 并注意$fileName将返回文件夹名称images/2Q3J1gU3j8NkMgt5HtGurSDtu5BIbeATNZb13Ih3.jpeg

$photo = new Photo;
      if ($request->hasFile('image_url')) {


               $fileName = $request->image_url->store('images');
               $photo->image_url =  $fileName;
            }

         $photo->title = $request->input('title');

         $photo->description = $request->input('description');
         $photo->category_id = $request->input('category_id');
         $photo->save();

         $request->session()->flash('create', $request->title. "Sekmingai sukurta");

         return redirect()->route('photos.index');