未读消息功能在Laravel中不起作用 [英] Unread message feature not working in Laravel

查看:60
本文介绍了未读消息功能在Laravel中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的Messenger处于未读"状态.当我尝试刷新数据库时,它会显示

I am trying to make an "unread" function for my messenger. When I refresh try to refresh the database it shows

表"messages"上没有名称为"read"的列.

There is no column with name 'read' on table 'messages'.

此错误和我的未读" 功能不起作用.我正在使用SQlite.

This error and my "unread" feature is not working. I am using SQlite.

这是我的未读迁移:-

public function up()
    {
        Schema::table('messages', function (Blueprint $table) {
            $table->boolean('read')->after('to')->default(false);
        });
    }

public function down()
    {
        Schema::table('messages', function (Blueprint $table) {
            $table->dropColumn('read');
        });
    }

此处是我的邮件迁移:-

public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->integer('from')->unsigned();
            $table->integer('to')->unsigned();
            $table->text('text');       
            $table->timestamps();
        });
    }

这是我的控制者:-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Friend;
use App\Message;
use App\Events\NewMessage;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ContactsController extends Controller
{
    public function __construct()
    {
       $this->middleware('auth');
    }

    public function index()
    {
        return view('home');
    }

    public function get(){
        $sem = Auth::user()->id;
        $contacts = DB::table('friends')
        ->where('my_id', $sem)
        ->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);
    }

    public function getMessagesFor($id)
    {
        $messages = Message::where('from', $id)->orWhere('to', $id)->get();
        $messages = Message::where(function($q) use ($id) {
            $q->where('from', auth()->id());
            $q->where('to', $id);
        })->orWhere(function($q) use ($id){
            $q->where('from', $id);
            $q->where('to', auth()->id());
        })
        ->get();
        return response()->json($messages);
    }

    public function send(Request $request)
    {   
        $message = Message::create([
            'from' => auth()->id(),
            'to' => $request->contact_id,
            'text' => $request->text
        ]);
        broadcast(new NewMessage($message));
        return response()->json($message);
    }
}

这是联系人ID的来源:-

public function up()
    {
        Schema::create('friends', function (Blueprint $table) {
            $table->id();
            $table->string('created_by');
            $table->string('my_id');
            $table->string('friends_id');
            $table->string('name');     
            $table->timestamps();
        });
    }

我似乎无法弄清问题,并且自从数周以来一直坚持下去,您的帮助将不胜感激.

I can't seem to figure out the problem and have been stuck with this since weeks, your help would really be appreciated.

推荐答案

有些事情可能会导致您的迁移问题.

There are a few things that may be causing you issues with your migration.

  • 您在SQL中有几个保留字(可能在SQLite)可能会导致问题.我赞成删除他们可能不会引起冲突的事物.

  • You have a couple of reserved words in SQL (thought maybe not in SQLite) that could be causing an issue. I would remove them in favor of something that is not potentially causing a conflict.

您可以将外键分配给数据库,这样它将起到很好的作用与您的模型.然后,您不必遍历整个过程 DB :: raw('from ... 的东西,它由模型自动分配如果您正确设置了关系.

You might assign foreign keys to the database so it will play nice with your models. Then you don't have to go through the whole DB::raw('from... stuff, it is automatically assigned by the model if you set up the relationships correctly.

我认为您遇到的问题是您可能正在使用迁移与预期有所不同.为什么不把布尔值字段上的原始迁移?如果此迁移未运行,或者正在运行以错误的顺序运行,或者以 down()模式运行,则该 read 字段将不在数据库表中.

I think the issue you are having though, is that you may be using the migration a bit differently that expected. Why not put the boolean field on the original migration? If this migration isn't run, or is run in the wrong order, or if it is run in down() mode, that read field will not be in the database table.

我建议从以下迁移开始进行测试.记下不同的(不冲突的)字段名称和自动递增字段:

I suggest to test, start with the following migration. Make note of the different (non-conflicting) field names and the auto-increment field:

Schema::create('messages', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('from_id')->unsigned();
        $table->integer('to_id')->unsigned();
        $table->boolean('has_read')->default(false);
        $table->text('text');       
        $table->timestamps();
    });

以此在数据库上运行新的迁移.重做您的控制器代码以使用新的字段名称并进行测试.如果这样可以解决错误:

Run a fresh migration on the database with this. Rework your controller code to work with the new field names and test. If this fixes the error:

表"messages"上没有名称为"read"的列.

There is no column with name 'read' on table 'messages'.

然后,我建议将外键添加到这样的ID中,例如:

Then I would recommend adding in the foreign keys to those ids like this for example:

$table->foreign('from_id')->references('id')->on('users'); 

但这是一个完全不同的问题,需要重新设计控制器的数据库绘制.现在,看看上面的迁移是否可以解决原始错误.

But this is a whole different issue and requires re-working of your controller db draws. For now, see if the above migration will solve the original error.

这篇关于未读消息功能在Laravel中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆