Iman Yasmin
Iman Yasmin

Reputation: 447

Laravel : Update field when value is exist

I have some problem with updating file. I have a form with the following attributes :

The problem is the update operation will save the pdf file as the following value :

It will include [""] to the pdf file value when updated.

I want the pdf file updated to a new file when a new file is selected, old file remained when there is no new file selected and null value to file when there is no file, to begin with.

Here is the update controller.

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');
    }
}

What am I doing wrong in this code? Please help me. Thank you.

Upvotes: 1

Views: 473

Answers (2)

Bhaskar
Bhaskar

Reputation: 19

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');
        }
    }

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163808

Change this:

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

To:

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

The thing is pluck() will return a collection of brochure, not a string. And value() will return a string or null.

Upvotes: 4

Related Questions