По какой-то причине мой "непрочитанный" метод не работает в моем чате - PullRequest
0 голосов
/ 27 мая 2020

Я изменил это Mseenger по моему желанию, и по какой-то причине непрочитанный метод не работает
Он не показывает никаких ошибок
Я думаю, проблема в контроллере

Вот ContactsController: -

public function get(){
        $sem = Auth::user()->id;
        $contacts = DB::table('friends')
    ->where('my_id', $sem)
    ->get();
    // $contacts = Friend::where('id', '!=', auth()->id())->get();

     // get a collection of items where sender_id is the user who sent us a message
        // and messages_count is the number of unread messages we have from him
        $unreadIds = Message::select(\DB::raw('`from` as sender_id, count(`from`) as messages_count'))
            ->where('to', auth()->id())
            ->where('read', false)
            ->groupBy('from')
            ->get();

            // add an unread key to each contact with the count of unread messages
        $contacts = $contacts->map(function($contact) use ($unreadIds) {
            $contactUnread = $unreadIds->where('sender_id', $contact->friends_id)->first();

            $contact->unread = $contactUnread ? $contactUnread->messages_count : 0;

            return $contact;
        });

    return response()->json($contacts);
}

Я заменил "$ contacts = Friend :: where ('id', '! =', Auth ( ) -> id ()) -> get (); " this с

$ sem = Auth :: user () -> id;
$ contacts = DB :: table ('friends') -> where ('my_id', $ sem) -> get ();
Это единственные изменения, которые я сделал в своем контроллере

Вот мой список контактов. vue

<template>
    <div class="contacts-list">
        <ul>
            <li v-for="contact in sortedContacts" :key="contact.friends_id" @click="selectContact(contact)" :class="{ 'selected': contact == selected }">
                <div class="avatar">
                    <img :src="contact.profile_image" :alt="contact.name">
                </div>
                <div class="contact">
                    <p class="name">{{ contact.name }}</p>
                    <p class="email">{{ contact.email }}</p>
                </div>
                <span class="unread" v-if="contact.unread">{{ contact.unread }}</span>
            </li>
        </ul>
    </div>
</template>

<script>
    export default {
        props: {
            contacts: {
                type: Array,
                default: []
            }
        },
        data() {
            return {
                selected: this.contacts.length ? this.contacts[0] : null
            };
        },
        methods: {
            selectContact(contact) {
                this.selected = contact;

                this.$emit('selected', contact);
            }
        },
        computed: {
            sortedContacts() {
                return _.sortBy(this.contacts, [(contact) => {
                    if (contact == this.selected) {
                        return Infinity;
                    }

                    return contact.unread;
                }]).reverse();
            }
        }
    }
</script>

Моя таблица: -

 public function up()
    {
        Schema::create('friends', function (Blueprint $table) {
            $table->id();
        $table->string('my_id');
        $table->string('friends_id');
        $table->string('name');     
            $table->timestamps();
        });
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...