How to solve this error Array to string conversion - in laravel inertia vue

41 Views Asked by At

I am facing an issue with my code when using multiselect. When I use multiselect, I encounter a problem, but when I don't use multiselect, the data is saved correctly. I am using Laravel, Vue.js, and Inertia.js.

This is my component:

<script setup>
import AdminLayout from "@/Layouts/AdminLayout.vue";
import { Head, Link, useForm } from "@inertiajs/vue3";
import VueMultiselect from 'vue-multiselect';
import { defineProps } from 'vue';

defineProps({
  employee: Array,
})
const form = useForm({
employee: [],
description: "",
 });
    <Head title="پرداخت معاش " />
    <div class="w-full mx-auto mt-6 p-6 bg-white shadow">
        <form @submit.prevent="form.post(route('salaries.store'))">
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
                <div class="col-md-4">
                    <label for="employee" class="block text-sm font-medium leading-6 text-gray-900">نام
                        </label>
                    <div class="mt-2">
                        <VueMultiselect
                            v-model="form.employee"
                            :options="employee"
                            :searchable="true"
                            :close-on-select="true"
                            :clear-on-select="false"
                            :preserve-search="false"
                            placeholder="انتخاب کنید"
                            track-by="id"
                            label="name"
                        />
                    </div>
                </div>
            <div class="col-md-12 mt-3">
                <InputLabel for="description" value="توضیحات" />
                <textarea rows="3" placeholder="توضیحات" class="w-full form-control" v-model="form.description"
                    required></textarea>
                <InputError class="mt-2" :message="form.errors.description" />
            </div>
            <div class="w-full flex items-center mt-4">
                <PrimaryButton class="ml-4" :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
                    ثبت معاش
                </PrimaryButton>
            </div>
        </form>

    </div>
</AdminLayout>

This is my controller code:

public function store(Request $request): RedirectResponse
{
    $validatedData = $request->validate([
        'employee' => 'required',
        'description' => 'required',
    ]);

    $salary = new Salary();
    $salary->employee_id = $validatedData['employee'];
    $salary->description = $validatedData['description'];

    $salary->save();

    return redirect()->route('salaries.index')->with('message', 'عملیه با موفقیت انجام شد.');
}

When I submit the data, I encounter the "Array to string conversion" error.

0

There are 0 best solutions below