0

I've decided to not use image in my create menu page but then I got this error "SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value".

This is the MenuController:

 public function store(Request $request, Menu $menu)
{
    $data = array();
    $data['name'] = $request->name;
    $data['slug'] = $request->slug;
    $data['price'] = $request->price;
    $data['status'] = $request->status;

    $menu = DB::table('menus')->insert($data);

    return redirect()->route('admin.menu.index');
}

and the add.blade.php:

 <form action="{{ route('admin.menu.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="form-group row">
                    <label for="staticEmail" class="col-sm-2 col-form-label">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="name" id="name" required value="{{ old('name') }}">
                    </div>
                </div>
                <div class="form-group row">
                    <label for="staticprice" class="col-sm-2 col-form-label">Price</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="price" id="price" required value="{{ old('price') }}">
                    </div>
                </div>
                <div class="form-group row">
                    <label for="staticprice" class="col-sm-2 col-form-label">Status</label>
                    <div class="col-sm-10">
                        <input type="radio" name="status" value="1" /> Active
                        <input type="radio" name="status" value="0" /> Not Active
                    </div>
                </div>
                <input id="slug" name="slug" type="hidden" value="Menu-{{ rand(100,999)}}" />
                <button type="submit" class="btn btn-secondary" style="margin-top: 20px; width: 100%">Submit</button>
            </form>

I don't know where did I do wrong.

aufa
  • 29
  • 1
  • 7

2 Answers2

1

Actually this erorr arise because in your table named menus, the column named image is not set to NULL by default.

Either you have to add some value (can be empty) in it or you have to update its structure and set NULL as by default value.

See the below image to help.

enter image description here

John Doe
  • 1,401
  • 1
  • 3
  • 14
0

As suggested above, setting "Default" to NULL will clear the error.

You can specify this when setting your migration table by

$table->string('image')->nullable();