0
  <form action="{{ route('associates_staff') }}" method="POST" class="card-title mb-0" style="margin-left: 5px">
                        @csrf
                        @method('PATCH')
                        <table class="table">
                            <thead>
                                <tr>
                                    <th class="d-none d-md-table-cell">Area e Subarea</th>
                                    <th class="d-none d-md-table-cell" style="text-align: center">Check Per Associare</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($selectA as $item)
                                    <tr>
                                        <td style="color:red;">Area Selezionata :
                                             {{ $item->nome_area }}
                                        </td>
                                        <td style="text-align: center">
                                            <input type="checkbox" name="selezionearea" value="{{ $item->nome_area }}" checked />
                                        </td>
                                    </tr>
                                @endforeach
                                @foreach ($selectS as $item)
                                    <tr>
                                        <td>Subarea : {{ $item->subareas }}</td>
                                        <td style="text-align: center">
                                            <input type="checkbox" name="subarea[]" value="{{ $item->subareas }}" />
                                        </td>
                                    </tr>
                                @endforeach
                                </tr>
                            </tbody>
                        </table>
                        <div id="buttinsert">
                            <input type="text" name="employee" value="{{ $search }}" hidden />
                            <button class="btn btn-lg btn-success" type="submit">
                                ASSOCIA
                            </button>
                        </div>
                        </form>

/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $subareas = $request->subarea;

    for ($i = 0; $i < count($subareas); $i++) {
        $subarea = $subareas;
        $area = $request->selezionearea;
        $employee = $request->employee;

        DB::insert(
            'insert into assoc (nome_area,employee,subarea)
        values (?,?,?)',
            [$area, $employee, $subarea]
        );

        DB::table('personale')->where('nome_cognome', $employee)->update(['status' => 'Assegnato']);

        return $this->index($request)->withErrors(['msgSuccess' => 'Associazione Completata']);
    }

error : Array to string conversion

6
  • Welcome to SOF, Moreno! Please edit your question to provide more details, such as what your problem is. Check out stackoverflow.com/help/how-to-ask for more details. Commented Apr 8, 2023 at 16:45
  • $subarea = $subareas[$i]; also, u can use buli insert for only one query to db, queries in the loop is not really good Commented Apr 8, 2023 at 16:46
  • $subarea = $subareas[$i]; I insert only the first value of the array and not all I would like id 1 and id 2 with subarea the different values Commented Apr 8, 2023 at 16:58
  • es: id 1 employee marco area magazzino subarea linea 1 Commented Apr 8, 2023 at 17:03
  • id 2 employee marco area magazzino subarea linea 2 Commented Apr 8, 2023 at 17:03

1 Answer 1

1

Because $subareas is an array, you can't insert to table. You should also insert multiple records to table one time. You can consider doing:

$insertedData = [];

foreach ($subareas as $subarea) {
    $insertedData[] = [
        'nome_area' => $request->selezionearea,
        'employee' => $request->employee,
        'subarea' => $subarea
    ];
}

DB::table('assoc')->insert($insertedData);

DB::table('personale')->where('nome_cognome', $request->employee)->update(['status' => 'Assegnato']);

return $this->index($request)->withErrors(['msgSuccess' => 'Associazione Completata']);
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much what I wanted.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.