0

I am new to Laravel. I have been trying to save an image to the database. Here is my controller method that I am trying for storing the image

    public function store(Request $request){
        //validation for form

        $validate= $request->validate([
            'name' => 'required|min:2|max:140',
            'position' => 'required|min:2|max:140',
            'salary' => 'required|min:2|max:140',
            'joining_date' => ''
        ]);


        //saving form

        if($validate){            
            $employee=new Employee;
            $employee->name =$request->input('name');
            $employee->company_name =$request->input('company_name');
            $employee->position =$request->input('position');
            $employee->salary =$request->input('salary');
            $employee->joining_date =$request->input('joining_date');
            $employee->user_id= auth()->user()->id;
            //image saveing method
            if($request->hasFile('image')){
                $image= $request->file('image');
                $filename = time() . '.' . $image->getClientOriginalExtension();
                Employee::make($image)->resize(300, 300)->save( public_path('/employee/images/' . $filename ) );

                $employee->image= $filename;

              }else{
                  return $request;
                  $employee->image= '';
              };

             $employee->save();

        //redirecting to Employee list
            return redirect('/employee/details')->with('success','Employee Added');
        }

I could save the form while there was no image and redirect it to the details page. but now when I try with the image, instead of saving it and redirecting to the details route, it returns me to the array of row of database like this:

{
  "_token": "FPHm9AKuEbRlqQnSgHhjPnCEKidi2xr0usgp7RoW",
  "name": "askfjlk",
  "company_name": "laksjsflkj",
  "position": "lkasjfkl",
  "salary": "35454",
  "joining_date": "4654-05-06",
  "image": "testing.png"
}

What did I do wrong here? please help me out this newb.

Kamran
  • 523
  • 6
  • 18
Zeed Tanue
  • 91
  • 5
  • 24
  • because you are returning the Request object from your Controller method .. this will cause it to return the inputs serialized as JSON ... apparently `$request->hasFile('image')` is false ... how are you sending this data? – lagbox Dec 23 '19 at 18:49
  • if ` $request->hasFile('image')` is false how am I supposed to check if there is any image or not? I am using form to send the data and `return redirect('/employee/details')->with('success','Employee Added');` using this to redirect. i dont understand how am I returning JSON array. @Kamran – Zeed Tanue Dec 23 '19 at 19:25

1 Answers1

0

You are returning $request object and Laravel does automatic JSON response.

if ($request->hasFile('image')){
    // image storing logic which obviously is never started because expression above is false
} else {
    return $request; // There is your problem
    $employee->image= '';
};

You need to check why you are getting false on $request->hasFile('image').

Also, one tip because you are new to Laravel:

// When you are accessing to $request object you can use dynamic propertes:
$employee->company_name = $request->input('company_name');
// is the same as
$employee->company_name = $request->company_name;

You can check it there: Laravel docs in the section: Retrieving Input Via Dynamic Properties