Php 未读邮件功能在Laravel中不起作用

Php 未读邮件功能在Laravel中不起作用,php,laravel,sqlite,Php,Laravel,Sqlite,我正在尝试为我的messenger创建一个“未读”功能。当我刷新时,尝试刷新显示的数据库 表“messages”上没有名为“read”的列 此错误和我的“未读”功能不起作用。我正在使用SQlite 这是我的未读迁移:- public function up() { Schema::table('messages', function (Blueprint $table) { $table->boolean('read')->after(

我正在尝试为我的messenger创建一个“未读”功能。当我刷新时,尝试刷新显示的数据库

表“messages”上没有名为“read”的列

此错误和我的“未读”功能不起作用。我正在使用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);
    }
}

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();
        });
    }
这是我的控制器:-

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

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

我似乎无法解决这个问题,而且几周来一直在处理这个问题,非常感谢您的帮助。

有一些事情可能会导致您的迁移出现问题

  • 您在SQL中有几个保留字(可能不在SQL中) SQLite),这可能会导致问题。我会取消他们的支持 指不可能引起冲突的事物

  • 您可以为数据库分配外键,这样它就可以很好地发挥作用 和你的模特们在一起。那你就不必经历整个过程了
    DB::raw('from…
    stuff),它由模型自动分配 如果您正确设置了关系

  • 我认为你现在面临的问题是,你可能正在使用 迁移与预期略有不同。为什么不将布尔值 原始迁移上的字段?如果此迁移未运行,或 以错误的顺序运行,或者如果在
    down()
    模式下运行,则
    读取
    字段将不在数据库表中

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

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();
    });
使用此在数据库上运行新的迁移。重新编写控制器代码以使用新的字段名并进行测试。如果此操作修复了错误:

表“messages”上没有名为“read”的列

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

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

但这是一个完全不同的问题,需要重新处理控制器db绘图。现在,请查看上述迁移是否能解决原始错误。

您是否运行了所有迁移?
php artisan migrate
?给我一点时间,让我再试一次是的,我做到了…..
php artisan migrate:status
看起来像什么?
是的|2020_05_22_082929_add_read_to_messages | 1
当然..我会尝试这种方法。你能建议我在ControllerSuite中进行更改吗?让我们先用一个简单的查询来测试一下,看看错误是否已通过新的迁移得到解决:试试这个
$unreadds=Message::where('to_id',auth()->id())->where('has_read',false)->get()
如果没有出现错误,您已经解决了问题。然后您可以尝试重新处理查询的其余部分。例如,只需在控制器中运行此项以进行测试-不要运行任何其他查询。让我们看看是否解决了问题。请稍等片刻,我很乐意为您提供帮助:)只需在其中一个答案上添加一条评论,我就会看到。您可以在看到它之后随时删除它。我认为这是最简单的方法。