oxbir
1 month ago
312
6
Laravel

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

Posted 1 month ago by oxbir

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

Please sign in or create an account to participate in this conversation.