Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zxywvu's avatar

Argument #1 ($values) must be of type array, int given, called

I have 3 tables.

public function up()
{
    Schema::create('legals', function (Blueprint $table) {
        $table->id();
        $table->foreignId('party_id')->constrained()->cascadeOnDelete();
        $table->text('legal_person');
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('reals', function (Blueprint $table) {
        $table->id();
        $table->foreignId('party_id')->constrained()->cascadeOnDelete();
        $table->text('real_person');
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('parties', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->constrained()->cascadeOnDelete();
        $table->foreignId('category_id')->constrained()->cascadeOnDelete();
        $table->timestamps();
    });
}

blade

<form action="{{ route('parties.updateAndCreate', auth()->id()) }}" method="post">
    @csrf
    @method('PUT')
    <input type="hidden" name="category_id" value="{{ $category->id }}">
    <p>
        <label>Add: </label>
        <a class="btn btn-lg btn-secondary me-2" id="addRealPerson">Real</a>
        <a class="btn btn-lg btn-secondary" id="addLegalPerson">Logal</a>
    </p>
    <div class="row" id="showRealPerson">
		
    </div>
</form>

script

<script>
    jQuery(function() {
        $(document).ready(function(){
            let count = 1;
            $('#addLegalPerson').click(function () {
                count++;
                addLegalPerson(count);
            });
            function addLegalPerson(number) {
                let html = ''+
                    '<div class="col-lg-6 mb-3">\n'+
                    '<fieldset class="border p-2">\n'+
                    '<legend class="float-none w-auto p-2 h6 fs-6 mb-0">legal_person</legend>\n'+
                    '<textarea name="parties['+number+'][legal_person][]" id="legal_person" class="form-control border-0" rows="10" aria-label="legal_person"></textarea>\n'+
                    '<a class="cursor-pointer text-secondary"><i class="fa-solid fa-2x fa-trash"></i></a>\n'+
                    '</fieldset>\n'+
                    '</div>';
                $('#showRealPerson').append(html);
            }
            $('#addRealPerson').click(function () {
                count++;
                addRealPerson(count);
            });
            function addRealPerson(number) {
                let html = ''+
                    '<div class="col-lg-6 mb-3">\n'+
                    '<fieldset class="border p-2">\n'+
                    '<legend class="float-none w-auto p-2 h6 fs-6 mb-0">real_person</legend>\n'+
                    '<textarea name="parties['+number+'][real_person][]" id="real_person" class="form-control border-0" rows="10" aria-label="real_person"></textarea>\n'+
                    '<a class="cursor-pointer text-secondary"><i class="fa-solid fa-2x fa-trash"></i></a>\n'+
                    '</fieldset>\n'+
                    '</div>';
                $('#showRealPerson').append(html);
            }
            showRealPerson();
            function showRealPerson() {
                let html = `@foreach($parties as $party)
                    @foreach($party->legals as $legal)
                    <div class="col-lg-6 mb-3">
                        <fieldset class="border p-2">
                            <legend class="float-none w-auto p-2 pb-0 h6 fs-6 mb-0">legal_person</legend>
                            <textarea name="parties[{{ $legal->id }}][legal_person][]" id="legal_person" class="form-control border-0" rows="10" aria-label="legal_person">{{ $legal->legal_person }}</textarea>
                            <a class="cursor-pointer text-secondary"><i class="fa-solid fa-2x fa-trash"></i></a>
                        </fieldset>
                    </div>
                    @endforeach
                    @foreach($party->reals as $real)
                    <div class="col-lg-6 mb-3">
                        <fieldset class="border p-2">
                            <legend class="float-none w-auto p-2 pb-0 h6 fs-6 mb-0">real_person</legend>
                            <textarea name="parties[{{ $real->id }}][real_person][]" id="real_person" class="form-control border-0" rows="10" aria-label="real_person">{{ $real->real_person }}</textarea>
                            <a class="cursor-pointer text-secondary"><i class="fa-solid fa-2x fa-trash"></i></a>
                        </fieldset>
                    </div>
                    @endforeach
                @endforeach`;
                $('#showRealPerson').append(html);
            }
            $(document).on('click', '.fa-trash', function() {
                $(this).parent().parent().parent().remove();
            });
        });
    });
</script>

web.php

Route::put('parties/updateAndCreate/{user}}', [App\Http\Controllers\PartyController::class, 'updateAndCreate'])->name('parties.updateAndCreate');

PartyController.php

public function updateAndCreate (Request $request)
{
    $parties = collect($request->input('parties'));
    $parties->each(function ($item) use ($request) {
        $party = Party::query()->create([
            'user_id' => auth()->id(),
            'category_id' => $request->input('category_id'),
        ]);
        if (isset($item['legal_person'])) {
            $party->legals()->updateOrCreate([
                'legal_person' => $item['legal_person'],
            ]);
        }
        if (isset($item['real_person'])) {
            $party->reals()->updateOrCreate([
                'real_person' => $item['real_person'],
            ]);
        }
    });
    Party::query()->whereNotIn('id', $parties->pluck('id'))->delete();
    return redirect()->back();
}

Party.php

class Party extends Model
{
	use HasFactory;

	protected $guarded = [];

	public function user()
	{
		return $this->belongsTo(User::class);
	}

	public function category()
	{
		return $this->belongsTo(Category::class);
	}

	public function legals()
	{
		return $this->hasMany(Legal::class);
	}

	public function reals()
	{
		return $this->hasMany(Real::class);
	}
}

The error comes here.

error

A user after login enters this page and can fill in the info in multiple dynamic forms.

In this form, a user can delete and save info and update, How to can do it?

0 likes
4 replies
MohamedTammam's avatar

As mentioned in the docs and in the error, the createOrUpdate method takes arrays as values not an int

https://laravel.com/docs/9.x/eloquent#upserts

Change your code to be something like that.

Real::updateOrCreate(
	['party_id' => $party->id],
	['real_person' => $item['real_person']]
);

Please or to participate in this conversation.