shahr's avatar
Level 10

SQLSTATE[HY000]: General error: 1364 Field 'sliders' doesn't have a default value

I have a multiple image. When I upload multiple image. I get this error.

SQLSTATE[HY000]: General error: 1364 Field 'sliders' doesn't have a default value

my blade

<form action="{{ route('design-studios.store') }}" method="post" enctype="multipart/form-data">
    @csrf
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
    </div>
   //......
    <div class="form-group">
        <fieldset class="border p-3">
            <legend>Slideshow</legend>
            <div class="form-group">
                <label for="sliders">Image</label>
                <input id="sliders" name="sliders[]" type="file" multiple class="form-control">
            </div>
        </fieldset>
    </div>
</form>

My Controller.

public function store(Request $request)
{
    $design_studio = new DesignStudio;
    $design_studio->user_id = 1;
    $design_studio->title = $request->title;
    $design_studio->lang = $request->lang;
    $design_studio->body = $request->body;
    if($request->has('image')) {
        $image = $request->file('image');
        $filename = $image->getClientOriginalName();
        $image->move(public_path('images/design-studio'), $filename);
        $design_studio->image = $request->file('image')->getClientOriginalName();
    }
    if ($sliders = $request->file('sliders')) {
        foreach ($sliders as $slider) {
            $filename = rand(111111, 999999);
            $extension = $slider->extension();
            $getFileExt = $filename . '.' . $extension;
            $slider->move(public_path('images/design-studios/'), $getFileExt);
        }
    }
    $design_studio->save();
    $design_studio->categories()->attach($request->category);
    return redirect()->route('design-studios.index');
}

My Model

protected $casts = [
    'sliders' => 'array'
];

I get this error.

error

0 likes
6 replies
siangboon's avatar

that's SQL error telling you that the "sliders" field is not a nullable and have no value assigned... where you assign value to "sliders" column?

shahr's avatar
Level 10

Yes, I gave her 4 photos to upload.

siangboon's avatar

am i missing something?? $sliders is a column or variable object??

eggplantSword's avatar

Show the code where you create the table in the database, the error is there.

Snapey's avatar

in this loop

    if ($sliders = $request->file('sliders')) {
        foreach ($sliders as $slider) {
            $filename = rand(111111, 999999);
            $extension = $slider->extension();
            $getFileExt = $filename . '.' . $extension;
            $slider->move(public_path('images/design-studios/'), $getFileExt);
        }
    }

you never write anything to the model, or create an array of sliders

Are the files created in the folder?

Please or to participate in this conversation.