arslan2037's avatar

I want to save Image name in db

I want to save image file name in db here is the function for upload image

        //file upload
public function imgUploader($FileToUpload)
{
    //define main image path
    $destinationPath = public_path(). '/uploads/img/';
    //define thumbnail path
    $thumbs = public_path(). '/uploads/img/';
    // $gallery = public_path(). '/uploads/items/gallery/';

    //get image
    $file = $FileToUpload;
    //rename image, remove spaces and converto to loawer case
    $filename =
        date('Y_m-d_His').'_'.
        strtolower(str_replace(' ', '_', $file->getClientOriginalName()));
    //move file to main image path
    $file->move($destinationPath, $filename);

    //generate thumbnail
    Image::make($destinationPath.$filename)->resize('640', '448')->save($destinationPath.$filename);
    //   Image::make($destinationPath.$filename)->resize('850', '371')->save($gallery.$filename);

    return $filename;
}

here is function for save data from this form

    public function updateCompanyProfile(Request $request)
{

    $companyprofile = CompanyProfile::where('company_id',$this->getCompanyID())->first();

    $companyprofile->business_card = $this->imgUploader();

    $input = Input::only('company_name', 'address_1', 'address_2', 'city', 'zip',
                         'state', 'country', 'area', 'phone', 'fax', 'email', 'website', 'business_card');
    try {
         $companyprofile->fill($input)->save();
         Session::flash('flash_message', 'Information Updated successfully!');
    } catch (ModelNotFoundException $e) {
        //    return view('pages.status')
        //      ->with('error',\Lang::get('profile.notYourProfile'))
        //    ->with('error_title',\Lang::get('profile.notYourProfileTitle'));
         Session::flash('flash_message', 'Error!!!');
    }
    return redirect::back();
}

how i can change it for save image in db and upload it on my folder as well

0 likes
12 replies
Snapey's avatar

why does it not work? on first glance it seems to be ok.

is business_card in your $fillable array?

by the way, there is a slug helper to make strings safe in urls etc

arslan2037's avatar

i change it like this and is working now but its not save image name in db

        $companyprofile = CompanyProfile::where('company_id',$this->getCompanyID())->first();

    if($request->file('business_card'))
    {
        $companyprofile->business_card = $this->imageUploaders($request->file('business_card'));
    }

    try {
        $companyprofile->update($request->all());
         Session::flash('flash_message', 'Information Updated successfully!');
    } catch (ModelNotFoundException $e) {
        //    return view('pages.status')
        //      ->with('error',\Lang::get('profile.notYourProfile'))
        //    ->with('error_title',\Lang::get('profile.notYourProfileTitle'));
         Session::flash('flash_message', 'Error!!!');
    }
    return redirect::back();
arslan2037's avatar

image name should be like one that is comming from imgUploader function but it's not

simondavies's avatar

in the call to the imgUploadermethod you are not passing in the data:

    $this->imgUploader()

You need to pass in the request var in order for the method to return a value back? Also in the

    $input = Input::only...

Looks like your overwriting the set one with the input value.

UPDATE to your update:

you are overwrting your inital setting of

$companyprofile->business_card = $this->image...

with

$companyprofile->update($request->all());

Set a variable for all, then if you need to replace that variable array value else carry on:

try {
    $record = $request->all();

    if($request->file('business_card'))
     {
            $record['business_card'] = $this->imageUploaders($request->file('business_card'));
        }

    $companyprofile->update($request->all());

}..

try somethgin like this not tried it so not too sure

arslan2037's avatar

only issue is that its not save image name instead of name its save C:\xampp\tmp\phpBEC9.tmp in business_card field

simondavies's avatar
Level 26

Sorry the last line should be

    $companyprofile->update($record);

and not the request all

So it shoudl be

try {
     $record = $request->all();

     if($request->file('business_card'))
        {
                $record['business_card'] = $this->imageUploaders($request->file('business_card'));
        }

    return $companyprofile->update($record);

}
simondavies's avatar

@arslan2037 sorry not sure what you mean, did you also notice i updated my last post, as i have an error in my code?

arslan2037's avatar

i want to say that when check it on dd it display file name but when i check it on db it save temp path not file name

Please or to participate in this conversation.