【问题标题】:I am trying to fetch messages from databse in larvavel (little stuck)我正在尝试从 laravel 中的数据库中获取消息(有点卡住)
【发布时间】:2020-12-09 01:01:08
【问题描述】:

我想在 laravel 中制作一个聊天应用程序。我正在尝试获取一对一的消息。

这是我的控制器:-

public function user( $id) {

        $chat = User::find($id);
        

        $table = DB::table('messages')
        ->where('sender', Auth::user()->id, '&&', 'reciever', $id)
        ->orWhere('sender', $id, '&&', 'reciever', Auth::user()->id)
        ->get();

        return view('user', compact('chat', 'id', 'table'));
   } 

我的消息迁移:-

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

路线如下:- Route::get('/user/{id}', 'ContactsController@user');

这是我得到的错误:- SQLSTATE[HY000]: General error: 1 near ""sender"": syntax error (SQL: select * from "messages" where reciever "sender" = 30 "sender" = 1)


我觉得我的逻辑有问题,谁能指导我。

【问题讨论】:

    标签: php mysql database laravel controller


    【解决方案1】:

    有关查询分组的信息,请参阅https://laravel.com/docs/7.x/queries#parameter-grouping。你需要像这样分离出你的逻辑

    $table = DB::table('messages')
        ->where(function($query) use ($id) { // Where (sender = User AND receiver = id)
               $query->where('sender', Auth::user()->id)
                    ->where('reciever', $id);
            })
        ->orWhere(function($query) use ($id) { // OR (sender = id AND receiver = User)
               $query->where('sender', $id)
                    ->where('reciever', Auth::user()->id);
        })
        ->get();
    

    【讨论】:

    • 这个结果是Illuminate\Support\Collection {#281 ▼ #items: [] } 但也检查了码头,但你能告诉我orWhere 中的那个“功能”有什么作用
    • 该函数称为Closure。将闭包传递到 where/orWhere 会创建查询组,当像这样混合 AND/OR 时,数据库需要分割 where 子句逻辑。
    【解决方案2】:

    问题在于您的where()orWhere()

    ->where('sender', Auth::user()->id, '&&', 'reciever', $id)
            ->orWhere('sender', $id, '&&', 'reciever', Auth::user()->id)
    

    你应该怎么做:

    ->where([
        ['sender', Auth::user()->id],
        ['reciever', $id],
    ])->orWhere([
        ['sender', $id],
        ['reciever', Auth::user()->id],
    ])
    

    我建议在深入研究之前阅读 Laravel Where Clauses 文档。

    【讨论】:

    • 这会导致一个空数组,但不能很好地检查文档
    • 确保预期的 sql 查询已执行并确保预期的记录在数据库中
    【解决方案3】:

    这对我有用:-

    $messages = Message::where('sender', $id)->orWhere('reciever', $id)->get();
        $messages = Message::where(function($q) use ($id) {
            $q->where('sender', auth()->id());
            $q->where('reciever', $id);
        })->orWhere(function($q) use ($id){
    
            $q->where('sender', $id);
            $q->where('reciever', auth()->id());
        })
        ->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多