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

Pardon, I don't understand what the error is? I'm trying create comment for topic. I got these error.
Argument #1 ($values) must be of type array, string given
Argument #1 ($values) must be of type array, string given
//

public ?array $data = [];
//

$record = Comment::create([
array_merge($data, [
'user_id' => auth()->id(),
'topic_id' => $this->topic->id,
]),
]);
//

public ?array $data = [];
//

$record = Comment::create([
array_merge($data, [
'user_id' => auth()->id(),
'topic_id' => $this->topic->id,
]),
]);
Thank you.
16 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shaung Bhone
Shaung BhoneOP2y ago
Typed property App\Http\Livewire\Forum\AddComment::$feeling must not be accessed before initialization
Typed property App\Http\Livewire\Forum\AddComment::$feeling must not be accessed before initialization
the error is changed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shaung Bhone
Shaung BhoneOP2y ago
protected function getFormSchema(): array
{
return [Forms\Components\Textarea::make('content')->required()];
}

public function replyFeeling()
{
abort_if(auth()->guest(), Response::HTTP_FORBIDDEN);

$data = $this->form->getState();

$record = Comment::create(
array_merge($data, [
'user_id' => auth()->id(),
'feeling_id' => $this->feeling->id,
]
)
);
//////
protected function getFormSchema(): array
{
return [Forms\Components\Textarea::make('content')->required()];
}

public function replyFeeling()
{
abort_if(auth()->guest(), Response::HTTP_FORBIDDEN);

$data = $this->form->getState();

$record = Comment::create(
array_merge($data, [
'user_id' => auth()->id(),
'feeling_id' => $this->feeling->id,
]
)
);
//////
$feeling = $topic
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
toeknee
toeknee2y ago
Did you try google? This is a php issue not filament. You need to test data is array so do: $data = $this->form->getState() ?? []; The feeling is because you need to mount feeling first
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Dan Harrin
Dan Harrin2y ago
if its a livewire component you should use mount()
Shaung Bhone
Shaung BhoneOP2y ago
<?php

namespace App\Http\Livewire\Forum;

use Filament\Forms;
use App\Models\Comment;
use App\Models\Feeling;
use Livewire\Component;
use Illuminate\Http\Response;
use Filament\Notifications\Notification;

class AddComment extends Component implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;

public Feeling $feeling;

public ?array $data = [];

public bool $showReplyModal = false;

public function mount(): void
{
$this->form->fill();
}

public function confirmFeelingReply(): void
{
$this->showReplyModal = true;
}

protected function getFormSchema(): array
{
return [Forms\Components\Textarea::make('content')->required()];
}

public function replyFeeling()
{
abort_if(auth()->guest(), Response::HTTP_FORBIDDEN);

$data = $this->form->getState();

$record = Comment::create(
array_merge($data, [
'user_id' => auth()->id(),
'feeling_id' => $this->feeling->id,
]
)
);

$this->form->model($record)->saveRelationships();

Notification::make()
->title('Commented successfully')
->success()
->send();

$this->reset('data');

$this->showReplyModal = false;

$this->emit('commentWasAdded');

return redirect()->back();
}

protected function getFormModel(): string
{
return Comment::class;
}

protected function getFormStatePath(): string
{
return 'data';
}

public function render()
{
return view('livewire.forum.add-comment');
}
}
<?php

namespace App\Http\Livewire\Forum;

use Filament\Forms;
use App\Models\Comment;
use App\Models\Feeling;
use Livewire\Component;
use Illuminate\Http\Response;
use Filament\Notifications\Notification;

class AddComment extends Component implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;

public Feeling $feeling;

public ?array $data = [];

public bool $showReplyModal = false;

public function mount(): void
{
$this->form->fill();
}

public function confirmFeelingReply(): void
{
$this->showReplyModal = true;
}

protected function getFormSchema(): array
{
return [Forms\Components\Textarea::make('content')->required()];
}

public function replyFeeling()
{
abort_if(auth()->guest(), Response::HTTP_FORBIDDEN);

$data = $this->form->getState();

$record = Comment::create(
array_merge($data, [
'user_id' => auth()->id(),
'feeling_id' => $this->feeling->id,
]
)
);

$this->form->model($record)->saveRelationships();

Notification::make()
->title('Commented successfully')
->success()
->send();

$this->reset('data');

$this->showReplyModal = false;

$this->emit('commentWasAdded');

return redirect()->back();
}

protected function getFormModel(): string
{
return Comment::class;
}

protected function getFormStatePath(): string
{
return 'data';
}

public function render()
{
return view('livewire.forum.add-comment');
}
}
toeknee
toeknee2y ago
Shaun, you need to add it to mount as Dan and myself have mentioned. There is no feeling->Id since feeling doesn’t exist. Define it in your mount function.
Shaung Bhone
Shaung BhoneOP2y ago
Yes. I defined it. Thank you. but now relationship problem here. damn.
QLSTATE[23502]: Not null violation: 7 ERROR: null value in column "feeling_id" of relation "comments" violates not-null constraint DETAIL: Failing row contains (505, 1, null, fasd asdfa sdfae, 2023-03-21 17:29:52, 2023-03-21 17:29:52). (Connection: pgsql, SQL: insert into "comments" ("content", "user_id", "feeling_id", "updated_at", "created_at") values (fasd asdfa sdfae, 1, ?, 2023-03-21 17:29:52, 2023-03-21 17:29:52) returning "id")
QLSTATE[23502]: Not null violation: 7 ERROR: null value in column "feeling_id" of relation "comments" violates not-null constraint DETAIL: Failing row contains (505, 1, null, fasd asdfa sdfae, 2023-03-21 17:29:52, 2023-03-21 17:29:52). (Connection: pgsql, SQL: insert into "comments" ("content", "user_id", "feeling_id", "updated_at", "created_at") values (fasd asdfa sdfae, 1, ?, 2023-03-21 17:29:52, 2023-03-21 17:29:52) returning "id")
Dan Harrin
Dan Harrin2y ago
please read and try to debug the error yourself before sending it to us looks like a migration or $fillable problem
Shaung Bhone
Shaung BhoneOP2y ago
Thank you. I'm trying my best.
toeknee
toeknee2y ago
You may find ChatGPT more useful if you provide it with good information to problem solve. Failing that google, then us 🙂
Shaung Bhone
Shaung BhoneOP2y ago
Thank you. Thank you all. I'm so sorry that I made you disappointed. I forgot to pass the data in the component.
<livewire:forum.add-comment :feeling="$feeling" />
<livewire:forum.add-comment :feeling="$feeling" />
So feeling_id gives me null. Thank you. This was helpful. And I don't need to add it mount when I use it like this.
public Feeling $feeling;
public Feeling $feeling;
toeknee
toeknee2y ago
You do, that just defines the type it doesn't add the values.
Want results from more Discord servers?
Add your server