skip to Main Content

I am confused about this error guys. I have no idea what is wrong here.

edit.blade:

<input type="file" name="image" class="form-control" placeholder="image">

Controller:

public function update(Request $request, $id)
{
    $sales = sales::find($id);

    $request->validate([
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    if ($request->hasFile('image')) {
        $destination_path = 'public/imagen/shop_logo';
        $image = $request->file('image');
        $image_name = $image->getClientOriginalName();
        $path = $request->file('image')->storeAs($destination_path, $image_name);

        $input['image']=$image_name;
    }

    $sales->image = $request->input('image');

    $sales->save();

From the source code above I do not see any error there. If you guys know I need some help here

3

Answers


  1. The error is what it says i.e. the image column on your sales table isn’t nullable.

    Then if you’re inputting image in the form and you’re still getting this error then make sure you’ve included enctype='multipart/form-data' in the form.

    Also how can you store image directly in the db? You’re supposed to store the image path in the db. Look at the following code for reference.

    use Image;
    
    $request->validate([
        'image' => 'required',
        'image.*' => 'mimes:jpg,jpeg,png'
    
    ]);
    
    $path = public_path('uploads/sliders/');
    if (!File::exists($path)) {
        File::makeDirectory($path, 0775, true);
    }
    
    $image = $request->file('image');
    $img_name = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
    Image::make($image)->resize( 847.5, 431 )->save('uploads/sliders/'.$img_name);
    $image_path = 'uploads/sliders/'.$img_name;
    
    Slider::create([
        'image' => $image_path
    ]);
    

    Here I’ve used Intervention Image library.

    Login or Signup to reply.
  2. Best Practice to store images in the table

    Validate your image

    
    $request->validate([
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);
    
    

    Upload a file to a specific location

    image is the input name which will come from the Form Request and make sure you have added enctype="multipart/form-data" in the form.

    
    $user = User::find(1);
    
    if ( $request->hasFile('image') ) {
        // upload file
        $destination_path = 'public/images/'; // <- your path
        $image = $request->file('image');
        $image_name = $image->getClientOriginalName(); // get file name
        $path = $image->storeAs($destination_path, $image_name);
    
        //Save file name in the table
        $user->image = $path;
    }
    
    $user->save(); // save the modal
    
    
    Login or Signup to reply.
  3. Assign the saved path of the image to the image attribute of the sales object.

    public function update(Request $request, $id)
    {
        $sales = sales::find($id);
    
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
    
        $destination_path = 'public/imagen/shop_logo';
        $image = $request->file('image');
        $image_name = $image->getClientOriginalName();
        $path = $request->file('image')->storeAs($destination_path, $image_name);
    
        $sales->image = $path;
    
        $sales->save();
    
        ...
    }
    

    You may retrieve uploaded files from an IlluminateHttpRequest instance using the file method or using dynamic properties.

    $file = $request->file('photo');
    
    $file = $request->photo;
    

    Documentation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search