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

Jakub003's avatar

Livewire Error: public property [message] must be of type: [numeric, string, array, null, or boolean].

I am getting this issue, and I don't understand what I am doing wrong as I did this same exact approach for another component on this site, and works perfectly....

ViewMessages.php

public $messages;

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

view-messages.blade.php

<div class="flex flex-col">
        @foreach ($messages as $message)
                {{$message->content}}
        @endforeach
</div>

Everything works and it outputs all the messages correctly.

When I try and pass in a livewire component into the for each, it gives that error.

@foreach ($messages as $message)
       @livewire('chat.show-message', ['message'=>$message], key('show-message-'.$message->id))
@endforeach

// ShowMessage.php
public $user;
    public $message;
    public $user_id;
    public $content;
  

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

Honestly am lost on what I am doing wrong, as I copied the exact same code and changed the variables that I used before. It works right now on the site when I do nested components.

<div class="flex flex-col space-y-4 py-4 overflow-y-auto">
     @foreach ($chats as $chat)
          @livewire('kanbans.show-sidebar-chat-message', ['chat'=>$chat], key('chat-'.$chat->id))
     @endforeach
</div>

I redid this component already twice, and can't find any syntax issues or spelling errors. =/

0 likes
9 replies
tykus's avatar

Is $message an Eloquent model; are you using Livewire v2?

Try typehinting it in the public properties as

public Message $message;

public function mount(Message $message)
{
	$this->message = $message;
}
Jakub003's avatar

In the laravel controller, I wasn't sure how to write this as an eqluent query

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class ChatController extends Controller
{

    public function view($user)
    {
        $user=User::where('id',$user)->firstOrFail();
        $auth_id = auth()->user()->id;

        $messages = DB::select('SELECT * FROM chat_messages 
                        WHERE (receiver_id = :auth_id and user_id = :user) OR (receiver_id = :user2 AND user_id = :auth_id2)',
                        ['auth_id' => $auth_id, 'user' => $user->id, 'auth_id2' => $auth_id, 'user2' => $user->id]);

        return view('backend.chat.view-contact-chat',compact('user','messages'));
    }
}

From here I pass on the data to the livewire component view-messages and when I break it up inside the for each loop, I wanted to pass the individual $message into a child livewire component show-message.

Jakub003's avatar

When I tried just doing this with the $user data it works fine, so it seems to be the fact that the $messages were not an eloquent query in the controller?

@foreach ($messages as $message)
   <div>
       @livewire('chat.show-message', ['user'=>$user], key('show-message'.$message->id))
   </div>
@endforeach
<?php

namespace App\Http\Livewire\Chat;

use Livewire\Component;

class ShowMessage extends Component
{

    public $user;

    public function mount($user)
    {
        $this->user = $user;
    }
    public function render()
    {
        return view('livewire.chat.show-message');
    }
}

Snapey's avatar

you don't actually run the messages query?

Jakub003's avatar

Inside the @foreach loop if i just output the messages it shows all of them

But when I try and child component, I can't pass in $message

Jakub003's avatar

I changed the query from using facade to this

public function view($user)
    {
        $user=User::where('id',$user)->firstOrFail();

        $messages = ChatMessage::where(function ($query){
                                $query->where('user_id', '=', auth()->id() )
                                        ->where('receiver_id', '=', 2 );
                                })->orWhere(function ($query){
                                    $query->where('user_id', '=', 2)
                                        ->where('receiver_id', '=', auth()->id() );
                                    })->get();
        
        return view('backend.chat.view-contact-chat',compact('user','messages'));
    }
    

and it fixed the errors.

Still trying to figure out how to change the 2 to be $user->id but making progress :P

apex1's avatar
apex1
Best Answer
Level 12

function ($query) use ($user) { ... }

1 like

Please or to participate in this conversation.