简体   繁体   中英

store path of photo in db in laravel

I am attempting to add a logo to a user profile, and I am doing this through two models (contractor and logo) and a one-to-one relationship.

I am getting the logo to upload properly to uploads/logos, but I am failing to store anything in my logo table.

I am note sure what I am overlooking, any suggestions:

my user controller: ContractorController.php *note: I am using dropzone.js in my view

public function logo_upload($id){

  $input = Input::all();
  $rules = array(
      'file' => 'image|max:3000',
  );

  $validation = Validator::make($input, $rules);

  if ($validation->fails())
  {
    return Response::make($validation->errors->first(), 400);
  }
    $file = Input::file('file');
    $destinationPath = 'uploads/logos';
    //$filename = $file->getClientOriginalName();
    $extension =$file->getClientOriginalExtension(); 
    $filename = str_random(12).".{$extension}";
    $upload_success = Input::file('file')->move($destinationPath, $filename);
    $path = Input::file('file')->getRealPath();

    if( $upload_success ) {
       return Response::json('success', 200);
    } else {
       return Response::json('error', 400);
    }

    if( $upload_success ) {
         $contractor = Contractor::find($id);
        $contractor->logo->name = Input::file('file')->getRealPath();
        $contractor->logo->contractor_id = $contractor;
        $contractor->save();
    }
}

My conractor model:

 function logo() {
    return $this->hasOne('Logo', 'contractror_id');
}

My logo model:

public function contractor() {
    return $this->belongsTo('Contractor', 'contractor_id');
}

You just swap the following code:

if( $upload_success ) {
   return Response::json('success', 200);
} else {
   return Response::json('error', 400);
}

// This code is out of reach if return occurs
if( $upload_success ) {
    $contractor = Contractor::find($id);
    $contractor->logo->name = Input::file('file')->getRealPath();
    $contractor->logo->contractor_id = $contractor;
    $contractor->save();
}

To something like following:

if( $upload_success ) {
    $contractor = Contractor::find($id);
    $logo = new Logo(array('name' => Input::file('file')->getRealPath()));
    $contractor->logo()->save($logo);
    return Response::json('success', 200);
} else {
   return Response::json('error', 400);
}

It's not reaching the code for saving the path in database because before that you are returning from the script so the code after the return is not getting executed. Btw, I just copied the code from your question and swapped it with small modification so not sure about your saving code whether it's working or not.

Update: Updated the code to save related model. Check how to save related models on documentation .

After you follow Heera instructions, you should also review how you are trying to store anything to your logo table. It should be something like this:

$contractor = Contractor::find($id);
$logo = new Logo(array('name' => Input::file('file')->getRealPath()));
$contractor->logo()->save($logo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM