【问题标题】:laravel select notifications for user by data fieldlaravel 按数据字段为用户选择通知
【发布时间】:2019-01-30 09:07:04
【问题描述】:

我有默认的 laravel 通知数据字段和默认的通知表 只尝试根据他的通知数据值为用户选择一些通知我已经在用户模型中创建了这个函数

public function notifications_data($col)
{
$notifications = $this->notifications()->where('data' , function ($q) use ($col){
            $q->where('appointment_id','0'); // query data as table
        })->get();

return ($notifications);

}

我在通知表 col 数据中保存了值 { "type":"Appointment", "appointment_id":"0", "date":null, "updated_by":"","status":"0" }

我如何获取所有状态 = 0 或约会 ID = 0 的通知

【问题讨论】:

    标签: json laravel notifications


    【解决方案1】:

    你也可以使用:

    public function notifications_data($col)
    {
      $notifications = $this->notifications()
        ->where('data->appointment_id', 0)
        ->orWhere('data->status', 0)
        ->get();
    
      return ($notifications);
    }
    

    【讨论】:

    • 哇,这是什么魔法?它是 Laravel 特定的还是 php 的东西?
    【解决方案2】:

    也许这行得通:

    public function notifications_data($col)
    {
      $notifications = $this->notifications()
        ->where('data','LIKE','%"appointment_id":"0"%')
        ->orWhere('data','LIKE','%"status":"0"%')
        ->get();
    
    return ($notifications);
    }
    

    【讨论】:

    • 我不确定这是否是正确的做法,但它确实有效!
    【解决方案3】:

    使用 Laravel 的查询构建器:

    public function notifications_data()
    {
        $notifications = DB::table('notifications')  //Specify the table you want to query
            ->where('status',0)    //Where status = 0
            ->orWhere('appointment_id',0)   //or appointment_id = 0
            ->get();
    
        return $notifications;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多