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

Desssha's avatar

preg_match(): Argument #2 ($subject) must be of typ0e string, array given - livewire index.blade.php

preg_match(): Argument #2 ($subject) must be of type string, array given (View: D:\xampp\htdocs\Chat_System\resources\views\conversations\index.blade.php)

i try use livewire but gime me this error problem in index.blade.php if i change anything not return data , how call it right please help

  <div class="card-body contacts_body">
                    <ui class="contacts">

                        <livewire:conversations.conversation-list :conversations="$conversations" />

                    </ui>
                </div>

second conversationController

class ConversationController extends Controller
{
    protected function index()
    {
        $conversations = Conversation::get();
        return view('conversations.index',compact('conversations'));
    }
}

livewire

class ConversationList extends Component
{

    public $conversations;

    public function mount(Collection $conversations)
    {
        $this->conversations = $conversations;
    }

    public function render()
    {
        return view('livewire.conversations.conversation-list');
    }
}

livewire blade


    @forelse($conversations as $conv)
    <li class="active">
        <div class="d-flex bd-highlight">
            <div class="img_cont">
                <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img">
                <span class="online_icon"></span>
            </div>
            <div class="user_info">
                <span>{{$conv->name != '' ? $conv->name : $conv->users->pluck('name')->join(', ')}}</span>
                <p>Disha is online</p>
            </div>
        </div>
    </li>
    @empty
        <div class="text-muted"> No Conversations Found </div>
    @endforelse
0 likes
40 replies
Nakov's avatar

In your Livewire blade, you have to have one root element.. so wrap it in a <span> for example:

<span>

    @forelse($conversations as $conv)
    <li class="active">
        <div class="d-flex bd-highlight">
            <div class="img_cont">
                <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img">
                <span class="online_icon"></span>
            </div>
            <div class="user_info">
                <span>{{$conv->name != '' ? $conv->name : $conv->users->pluck('name')->join(', ')}}</span>
                <p>Disha is online</p>
            </div>
        </div>
    </li>
    @empty
        <div class="text-muted"> No Conversations Found </div>
    @endforelse
</span>
Nakov's avatar

@Desssha do you see the code that I showed you? It contains one more element.. so your @forelse is wrapped within a <span> because for livewire to correctly render you have to have ONE root element, while your loop will create as many as there are conversations.

Desssha's avatar

@Nakov already i put it inside and i try your code with same problem

<div>

    @forelse($conversations as $conv)
    <li class="active">
        <div class="d-flex bd-highlight">
            <div class="img_cont">
                <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img">
                <span class="online_icon"></span>
            </div>
            <div class="user_info">
                <span>{{$conv->name != '' ? $conv->name : $conv->users->pluck('name')->join(', ')}}</span>
                <p>Disha is online</p>
            </div>
        </div>
    </li>
    @empty
        <div class="text-muted"> No Conversations Found </div>
    @endforelse

</div>
Nakov's avatar

@Desssha is this:

  <div class="card-body contacts_body">
                    <ui class="contacts">

                        <livewire:conversations.conversation-list :conversations="$conversations" />

                    </ui>
                </div>

the only thing that you have in your index.blade.php because the error is there, but I don't think that it is related to this code.

If you remove the <livewire.. will the page load, or you will still get the same error?

Desssha's avatar

@Nakov when i change in it line livewire pag load but not return data from database .

Nakov's avatar

@Desssha Can you show the component, how do you import the Collection?

if you change this:

public function mount(Collection $conversations)

to this:

public function mount($conversations)

are you still getting the error?

Nakov's avatar

@Desssha I feel like a policeman, for each step I got to milk out of you to share something more that is going on.

I will suggest that you remove line by line things that you added, until you find which line causes the error and focus on fixing that first.

You can start by removing this:

<span>{{$conv->name != '' ? $conv->name : $conv->users->pluck('name')->join(', ')}}</span>

do you still get an error... and so on.. That's called debugging.

Desssha's avatar

@Nakov i already do nothing change problem in index.blade.php

                        <livewire:conversations.conversation-list :conversations="$conversations" />

when change here work without problen and no data and i have data in database,

Nakov's avatar

@Desssha Then try this:

remove this from your controller:

$conversations = Conversation::get();

and initialize it in the component:

<livewire:conversations.conversation-list  />
public function mount()
{
	$this->conversations = Conversation::get();
}
Desssha's avatar

@Nakov sir same problem , why this first time use livewire and hat it but need to do by livewire

Snapey's avatar

why are you running db queues inside a loop in the blade file?

Desssha's avatar

@Snapey so where should do it? in video do the same and worked normal, in index blade?

Snapey's avatar

@Desssha at least eager load the users

regarding your issue, what's the full error message?

Desssha's avatar

@Snapey

    <livewire:conversations.conversation-list :conversations="$conversations" />

problem here sir when i change it work but no data from database

Desssha's avatar

i solved it , index.blade.php i remove all just call livewire like it

  <div class="card-body contacts_body">
                    <ui class="contacts">

                        <livewire:conversations.conversation-list/>
                    </ui>
                </div>

and move all code inside livewire and change collection -> Conversation model

namespace App\Http\Livewire\Conversations;

use App\Models\Conversation;
use Illuminate\Support\Collection;
use Livewire\Component;

class ConversationList extends Component
{

    public $conversations;

    public function mount(Conversation $conversations)
    {
        $this->conversations = $conversations;
    }


    public function render()
    {
        $conversations = Conversation::get(); 
        return view('livewire.conversations.conversation-list');
    }
}

problem was in Collection in function mount , it's all and work hope can help any one else face the same problem,

Nakov's avatar

@Desssha this is not a solution :) and btw I told you in my last answer above to remove the parameter and move the logic into the component..

You are doing this:

 public function render()
    {
        $conversations = Conversation::get(); 
        return view('livewire.conversations.conversation-list');
    }

assigning a local variable which is NEVER used :) since you don't pass that one to the view, but your view uses the public $conversations which is assigned to null since you are not passing anything to your component..

so basically this will ACTUALLY work:

public function mount()
{
	$this->conversations = Conversation::get();
}

public function render()
{
    return view('livewire.conversations.conversation-list');
}
Desssha's avatar

@Nakov solved like i show up but give me new error Attempt to read property "users" on bool (View: D:\xampp\htdocs\Chat_System\resources\views\livewire\conversations\conversation-list.blade.php) not back name try by relation between tables not help also

Nakov's avatar

@Desssha you solved it but you have issues :)

Why am I even trying, right?

Good luck!

Desssha's avatar

@Nakov sir give me the sam error TypeError preg_match(): Argument #2 ($subject) must be of type string, array given (View: D:\xampp\htdocs\Chat_

nothing change not work ,

Desssha's avatar

@Nakov i do your code but give me the old error , don't be made i try 2 days now in it can't complete project

Nakov's avatar

@Desssha I gave you steps how to debug it above. It cannot keep showing the same error if you just follow what I am saying. There are TONS of videos and documentation for you to learn from.. just takes time..

2 likes
Desssha's avatar

@Nakov take a look please

<?php

namespace App\Http\Livewire\Conversations;

use App\Models\Conversation;
use Illuminate\Support\Collection;
use Livewire\Component;

class ConversationList extends Component
{

    public $conversations;

    public function mount()
    {
        $this->conversations = Conversation::get();
        
    }

    public function render()
    {
        return view('livewire.conversations.conversation-list');
    }
}

controller

<?php

namespace App\Http\Controllers\Conversatins;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ConversationController extends Controller
{
    protected function index()
    {

        return view('conversations.index');
    }
}

and index.blade.php

  <div class="card-body contacts_body">
                    <ui class="contacts">

                        <livewire:conversations.conversation-list/>

                    </ui>
                </div>

and view component


<div>

    @forelse($conversations as $conv)
    <li class="active">
        <div class="d-flex bd-highlight">
            <div class="img_cont">
                <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img">
                <span class="online_icon"></span>
            </div>
            <div class="user_info">
                <span>{{$conv->name != '' ? $conv->name : $conv->users->pluck('name')->join(', ')}}</span>
                <p>Disha is online</p>
            </div>
        </div>
    </li>
    @empty
        <div class="text-muted"> No Conversations Found </div>
    @endforelse

</div>
Desssha's avatar

@Nakov first one

<?php

namespace App\Http\Livewire\Conversations;

use App\Models\Conversation;
use Illuminate\Support\Collection;
use Livewire\Component;

class ConversationList extends Component
{

    public $conversations;

    public function mount()
    {
        $this->conversations = Conversation::get();

    }

    public function render()
    {
        return view('livewire.conversations.conversation-list');
    }
}

Desssha's avatar

@Nakov u need livewire or model ?? this model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Conversation extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $dateFormat = ['last_message_at'];

    protected function users()
    {
        return $this->belongsToMany(User::class);
    }
}
Nakov's avatar

@Desssha Why would I need something that you already shared?

Livewire Component is called ConversationList, the model is called Conversation.. I am asking for Conversation so that's the MODEL of course.. which you never shared..

Desssha's avatar

@Nakov

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Conversation extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $dateFormat = ['last_message_at'];

    protected function users()
    {
        return $this->belongsToMany(User::class);
    }
}
Nakov's avatar

@Desssha I don't see what the error can be.. and unfortunately I don't have your code to try it out. I said it once above.. I will say it again.. if you remove this line:

<span>{{$conv->name != '' ? $conv->name : $conv->users->pluck('name')->join(', ')}}</span>

from the Livewire view, do you still see the same error ? Just remove it to try and then return it back. If you don't see the error, then you'll know which code causes the issue and maybe find a solution for it.

Run : php artisan view:clear in case you have some caching issue.. or I don't know what to say.. I would also try to create a NEW component and do things line by line.. That's all I can help with.

Snapey's avatar

@Desssha You would do better if you tried to understand what is being asked

Desssha's avatar

@Nakov i do really i deleted and clear caching and view and restart server same error , i can give u githup please if u can help me , i wast 2 days in it and not solved

Desssha's avatar

@Snapey sir i try all and like he told me same error every time i show all my code there nothing diffrent,

Nakov's avatar

@Desssha if you call this waste, you are not for a programmer's job :) if it is a public repository give me the url..

Nakov's avatar
Nakov
Best Answer
Level 73

@Desssha Remove this line from your model:

    protected $dateFormat = ['last_message_at'];

That's not a dateFormat..

You can make it :

    protected $dates = ['last_message_at'];

that's what solved it https://snipboard.io/uHQWfL.jpg

1 like
Desssha's avatar

@Nakov OMG it work and name show thanks a lot sir , thanks for your time it not first time help me i really grateful .

Please or to participate in this conversation.