【问题标题】:Laravel - File Input Array UpdateLaravel - 文件输入数组更新
【发布时间】:2019-04-27 17:53:06
【问题描述】:

我想更新我的文件输入图像,但是一旦我更新了 1 个图像,其他图像就会被删除,这是为什么呢?我想让它们保持上传时的状态。请帮帮我,谢谢。

Here is an image of the problem

控制器

更新,公共函数,这里是我放代码逻辑的地方

public function update(Request $request, $id)
{
          $this->validate($request, [
        'inflightmagz_date' => 'required',
        'infightmagazine_pdf.*' => 'image|nullable|max:1999'
    ]);

    $inflightmags = [];

    if ($request->has('infightmagazine_pdf'))
    {   
        //Handle File Upload


        foreach ($request->file('infightmagazine_pdf') as $key => $file)
        {
            // Get FileName
            $filenameWithExt = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $file->storeAs('public/infightmagazine_pdfs',$fileNameToStore);
            array_push($inflightmags, $fileNameToStore);
        }

        $fileNameToStore = serialize($inflightmags);
    }



    $inflightmagContent =  InflightMagazine::find($id);
    $inflightmagContent->inflightmagz_date = $request->input('inflightmagz_date');
       foreach ($inflightmags as $key => $value) {

        $implodedInflight = implode(' , ', $inflightmags);
        if($request->hasFile('infightmagazine_pdf')){
        $inflightmagContent->infightmagazine_pdf = $implodedInflight;
        }

    }
    $inflightmagContent->save();
    return redirect('/admin/airlineplus/inflightmagazines')->with('success', 'Content Updated');
}

查看、编辑.blade.php

  {!! Form::open(['action'=>['Admin\FleetsController@update',$fleet->id], 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}

        <div class="table-responsive">
          <table class="table table-bordered" id="dynamic_field">
            <tr>
              <td>   {{Form::text('title', $fleet->title, ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}<br>

                {{Form::textarea('description', $fleet->description, ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br>
                <div class="card card-body col-md-8">

                @foreach(explode(' , ' ,$fleet->fleet_image) as $content)
                  <img src="{{ asset('storage/fleet_images/' . $content) }}" style="width:50px;height:50px;"><br/>

                  {{ Form::file('fleet_image[]',['id'=>'exampleFormControlFile1']) }}<br/>
                  @endforeach 
                </div>
             </td>

            </tr>
          </table>
          {{Form::hidden('_method', 'PUT')}}
          {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
        </div>
    {!! Form::close() !!}

【问题讨论】:

  • b'因为你只上传了一张图片,另外两张图片是空的,所以它的更新为空,这就是为什么你的两张图片被更新为空。因此,您需要检查图像是否具有空值而不是没有更新,并保持其旧值不变。
  • 我该怎么做?好逻辑先生。语法如何?先生,您可以编辑我的代码吗?我非常需要它来工作
  • 检查hasFile里面的for循环
  • 你能编辑我的代码先生吗:(所以你可以让我投票并标记你的答案:(

标签: php arrays laravel laravel-5 eloquent


【解决方案1】:

试试这个代码

添加 if 条件,如if($request-&gt;hasFile('infightmagazine_pdf') &amp;&amp; !empty($implodedInflight) &amp;&amp; isset($implodedInflight)) this

//Handle File Upload
foreach ($request->file('infightmagazine_pdf') as $key => $file)
{
    if ($file->has('infightmagazine_pdf'))
    {  
        // Get FileName
        $filenameWithExt = $file->getClientOriginalName();
        //Get just filename
        $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
        //Get just extension
        $extension = $file->getClientOriginalExtension();
        //Filename to Store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;
        //Upload Image
        $path = $file->storeAs('public/infightmagazine_pdfs',$fileNameToStore);
        array_push($inflightmags, $fileNameToStore);
        $fileNameToStore = serialize($inflightmags);
    }
} 

$inflightmagContent =  InflightMagazine::find($id);
    $inflightmagContent->inflightmagz_date = $request->input('inflightmagz_date');
       foreach ($inflightmags as $key => $value) {

        $implodedInflight = implode(' , ', $inflightmags);
        if($request->hasFile('infightmagazine_pdf') && !empty($implodedInflight) && isset($implodedInflight)){
        $inflightmagContent->infightmagazine_pdf = $implodedInflight;
        }

    }
    $inflightmagContent->save();
    return redirect('/admin/airlineplus/inflightmagazines')->with('success', 'Content Updated');

【讨论】:

  • 还是一样的先生:(它不起作用:(我插入了两个图像,当我更新它时删除了另一个图像:(
  • for loop之外打印你的$fileNameToStore数组,看看结果如何
  • 等待。它抛出一个错误说.. Method Illuminate\Http\UploadedFile::has 不存在。
  • 嘿嘿抱歉我不知道打印的语法请告诉我:)
  • echo "&lt;pre&gt;;print_r(array);"
猜你喜欢
  • 2019-04-27
  • 2018-12-13
  • 2020-10-18
  • 2016-10-15
  • 2021-11-04
  • 2013-11-18
  • 2015-05-31
  • 2018-12-09
  • 2021-07-18
相关资源
最近更新 更多