1

Here is my controller code :

 public function save(Request $request) {
    try {

        $this->validate($request, Country::rules());


        If(Input::hasFile('flag_image')){
            $file = Input::file('flag_image');
            $destinationPath = public_path(). '/images/admin/country/';
            $filename = $file->getClientOriginalName();
            $image = time().$filename;
            $file->move($destinationPath, $image);
            $imgpath = 'public/images/admin/country/'.$image;
        }
        $request['flag_image'] = $imgpath;
        $country = Country::saveOrUpdate($request);
        if($country !== false) {
            return redirect()->route('lists-country')->with('success', trans('Country data added successfully.!!'));
        } else {
            return back()->with('error', "Unable to save country data.!!")->withInput();
        }
    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save country data.!!")->withInput();
    }
}

When I dd($request) before saveOrUpdate function I got the full path of the image uploaded file.

 +request: ParameterBag {#40 ▼
#parameters: array:6 [▼
  "_token" => "c94UG3R2PbbdHsXRLzs9OjEBTna23OHINFpki63U"
  "id" => ""
  "title" => "INDIA"
  "short_name" => "IN"
  "status" => "Active"
  "flag_image" => "public/images/admin/country/1515577652banner5.jpg"
]}

After successfully adding data on table a flag_image path look like "D:\xampp\tmp\php63D3.tmp" i want to store "public/images/admin/country/1515577652banner5.jpg" this path.

1
  • Try: $country = Country::saveOrUpdate($request->all()); instead of $country = Country::saveOrUpdate($request); Hope this will fixed it! Commented Jan 10, 2018 at 9:55

2 Answers 2

3

Just do

$country->flag_image = $imgpath;
$country->save();

after

$country = Country::saveOrUpdate($request);
Sign up to request clarification or add additional context in comments.

6 Comments

working thanx...but didnt understand what ll happen.?
@Javed with this code you'll update the table twice. Your saveOrUpdate() method will update the data and then save() will create another query to update flag_image
because you are registering everything in the request and the request has flag_image so if you want to prevent that just add $request->except('flag_image') to avoid that
as @Alexey Mezenin mention this will update twice if you want to avoid that just merge the array of the request with flag_image path like this Country::saveOrUpdate(array_merge($request->except('flag_image'), 'flag_image' , $imgpath) );
@Javed you should choose the best answer. If you'll use BM2ilabs' code tick his answer. If you'll use my code, tick my answer.
|
2

You need to merge() it:

$request->merge(['flag_image' => $imgpath]);

Instead of:

$request['flag_image'] = $imgpath;

1 Comment

thanks for sharing this one . Merge looks much cleaner , i didn't know about this one ,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.