Laravel:值存在时更新字段

时间:2017-12-27 07:52:24

标签: php laravel

我在更新文件时遇到了一些问题。我有一个具有以下属性的表单:

  • title
  • text
  • pdf file

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

  • with file: [“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');
    }
}

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

2 个答案:

答案 0 :(得分:4)

改变这个:

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

要:

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

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

答案 1 :(得分: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');
        }
    }