1

I want to insert image and pdf into mysql database with updateOrCreate()

I tried this but it returns 1 or 0 instead of actual file ..

$info = Info::updateOrCreate(
        ['user_id' => Auth::id()],

        [
         'image' => $request->hasFile('image') && $request->image->move(public_path('uploads'),$request->full_name.'.'.$request->image->getClientOriginalExtension()),
         'cv' => $request->hasFile('cv') && $request->cv->move(public_path('uploads'),$request->user_id.'.'.$request->cv->getClientOriginalExtension())
        ]
    );
2
  • 1
    Because you're checking for if-and-move-was-successful, which only returns true or false. Move your if-and-move lines out of the upadteOrCreate, and assign those variables properly. Commented Dec 2, 2021 at 18:49
  • I'am new these things .. May I ask how should it to be done ? Commented Dec 2, 2021 at 19:05

1 Answer 1

1

Based on your code, you are assigning the values of image and cv by the result of 'hasFile && move_uploaded_file' which returns true or false (0 or 1). what you should do is if has_file and move_uploaded_file returns true, return the file path location. if not, leave it blank.

$info = Info::updateOrCreate(
['user_id' => Auth::id()],

[
 'image' => $request->hasFile('image') && $request->image->move(public_path('uploads'),$request->full_name.'.'.$request->image->getClientOriginalExtension()) ? public_path('uploads').$request->full_name.'.'.$request->image->getClientOriginalExtension() : "",
 'cv' => $request->hasFile('cv') && $request->cv->move(public_path('uploads'),$request->user_id.'.'.$request->cv->getClientOriginalExtension()) ? public_path('uploads').$request->user_id.'.'.$request->cv->getClientOriginalExtension() : ""
]
);

the code above only returns the string of the file location where you uploaded the file (from move_uploaded_file). if you want to insert it directly to the database, you need a blob/binary column and insert the blob to the database using mysql stmt (prepared statement).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but Gives syntax errors like "unexpected , "
i edited the code. kindly try the updated code again.

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.