Laravel:值存在时更新字段

问题描述 投票:1回答:2

我有更新文件的问题。我有一个具有以下属性的表单:

  • 标题
  • 文本
  • pdf文件

问题是更新操作会将pdf文件保存为以下值:

  • 与文件:[“example.pdf”]
  • 没有文件 : [””]

更新时,它将包含[“”]到pdf文件值。

我希望在选择新文件时将pdf文件更新为新文件,在没有选择新文件时保留旧文件,在没有文件时保留为空文件值。

这是更新控制器。

public function update()
{
    if (Auth::check()) {
        $user_id = Auth::user()->id;    
        $main_id = Input::get('edit_id');
        $announcements = Announcement::find($main_id);
        $getFile = Input::file('new_brochure');

        $rules = array(
        'title' => 'required',
        'text' => 'required',
        );

        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {
        return back()->withInput()
        ->withErrors($validator)
        ->withInput();
      }

      if ($getFile) {
        $file = array('new_brochure' => Input::file('new_brochure'));
        $destinationPath = 'img/brochures/announcements'; // upload path
        $extension = Input::file('new_brochure')->getClientOriginalExtension(); 
        $fileName = rand(11111,99999).'.'.$extension; // renaming image
        Input::file('new_brochure')->move($destinationPath, $fileName);
        $announcements->brochure = $fileName;
    }

    $old = Announcement::where('id',$main_id)->pluck('brochure');
    if (empty($old)) {
        $announcements->brochure = null;
    }
    else {
        $announcements->brochure = $old;
    }

    $announcements->title = (Input:: get('title'));
    $announcements->from = (Input:: get('from'));
    $announcements->to = (Input:: get('to'));
    $announcements->text = (Input:: get('text'));
    $announcements->is_active = '1';
    $announcements->created_by = $user_id;
    $announcements->updated_by =  $user_id;
    $current_date = date("Y-m-d H:i:s");
    $announcements->created_at = $current_date.".000";
    if ($announcements->save()){
        $this->request->session()->flash('alert-success', 'Updated successfully!');
    }
    else{
         $this->request->session()->flash('alert-warning', 'Could not update!');
        }
        return redirect()->route('Announcements_view');
    }
}

我在这段代码中做错了什么?请帮我。谢谢。

php laravel
2个回答
4
投票

改变这个:

$old = Announcement::where('id',$main_id)->pluck('brochure');

至:

$old = Announcement::where('id',$main_id)->value('brochure');

事情是pluck()将返回brochure的集合,而不是字符串。而value()将返回一个字符串或null


0
投票
public function update()
    {
        if (Auth::check()) {
            $user_id = Auth::user()->id;    
            $main_id = Input::get('edit_id');
            $announcements = Announcement::find($main_id);
            $getFile = Input::file('new_brochure');

            $rules = array(
            'title' => 'required',
            'text' => 'required',
            );

            $validator = Validator::make(Input::all(), $rules);
            if ($validator->fails()) {
            return back()->withInput()
            ->withErrors($validator)
            ->withInput();
          }

          if (!empty(Input::file('new_brochure'))) {
            $file = array('new_brochure' => Input::file('new_brochure'));
            $destinationPath = 'img/brochures/announcements'; // upload path
            $extension = Input::file('new_brochure')->getClientOriginalExtension(); 
            $fileName = rand(11111,99999).'.'.$extension; // renaming image
            Input::file('new_brochure')->move($destinationPath, $fileName);
            $announcements->brochure = $fileName;
        }
        else

            $old = Announcement::where('id',$main_id)->value('brochure');
            $announcements->brochure = $old;
        }

        $announcements->title = (Input:: get('title'));
        $announcements->from = (Input:: get('from'));
        $announcements->to = (Input:: get('to'));
        $announcements->text = (Input:: get('text'));
        $announcements->is_active = '1';
        $announcements->created_by = $user_id;
        $announcements->updated_by =  $user_id;
        $current_date = date("Y-m-d H:i:s");
        $announcements->created_at = $current_date.".000";
        if ($announcements->save()){
            $this->request->session()->flash('alert-success', 'Updated successfully!');
        }
        else{
             $this->request->session()->flash('alert-warning', 'Could not update!');
            }
            return redirect()->route('Announcements_view');
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.