简体   繁体   中英

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. =/

The issue was with the DB Facade query

$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]);

Although not sure yet how to pass in the $user->id but when I set the user id to #2, the livewire components work as intended.

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'));
    }
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM