EmiaNakib's avatar

Why image upload not working

I want to upload image into public folder and database in laravel but I am facing some problem, Image is uploading into public folder but image is not saving into database. Can anyone help me telling where it is going wrong? Here is my code.

controller

public function clientaction(Request $request)
   {


        $validation = validator::make($request->all(),[
          'select_file' =>'required|image|mimes:jpeg,png,jpg,gif|max:2048'
        ]);
        if($validation->passes())
        {
         $image = $request->file('select_file');
         $new_name = rand() . '.' . $image->getClientOriginalExtension();
         $image->move(public_path('images'), $new_name);
         return response()->json([
          'message'   => 'Image Upload Successfully',
          'class_name'  => 'alert-success'
         ]);


        }
        else
         {
          return response()->json([
          'message'   => $validation->errors()->all(),
         'class_name'  => 'alert-danger'
      ]);
     }

     DB::table('logo-clients')->insert([

      'select_file'=> $new_name
  ]);
   }

Route

Route::get('DashBoard','AdminController@DashBoard');


Route::get('Clients','AdminController@clients');
Route::post('/ajax_upload/action','AdminController@clientaction')->name('ajaxupload.action');
0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@emianakib

I guess it's because you already return on when the validation is passed.

Please look at your code you returned twice on validation passed and on failed.

I suggest you move your code on saving it to the database before you return the response()->json() when the validation is passed.

1 like

Please or to participate in this conversation.