php - 可以将数组值存储在数据库中并在 Laravel 中使用 where 子句吗?

标签 php arrays laravel

我想将数组类型存储在数据库的一列中,然后使用 where to compare array($gID) 之间的 n_group_code_id 选择它 如下函数。

CREATE TABLE IF NOT EXISTS `tb_notification` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `n_user_id` int(10) NOT NULL,
  `n_group_code_id` varchar(30) NOT NULL,
  `n_source_id` int(10) NOT NULL,
  `n_activity_type` varchar(100) NOT NULL,
  `n_create_times` datetime NOT NULL,
  `n_description` varchar(160) NOT NULL,
  `n_status` int(2) NOT NULL,
  `url` varchar(200) NOT NULL,
  `updated_at` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=542 ;

--
-- Dumping data for table `tb_notification`
--

INSERT INTO `tb_notification` (`id`, `n_user_id`, `n_group_code_id`, `n_source_id`, `n_activity_type`, `n_create_times`, `n_description`, `n_status`, `url`, `updated_at`, `created_at`) VALUES
(535, 66, '[7,11,11,11]', 1, 'Issue Till', '2016-04-25 07:04:54', 'Issue Till', 0, '0', '2016-04-25 07:59:54', '2016-04-25 07:59:54'),
(536, 66, '[7,11,11,11]', 1, 'Issue Till', '2016-04-25 08:04:47', 'Issue Till', 0, '0', '2016-04-25 08:15:47', '2016-04-25 08:15:47'),
(537, 66, '[7,11,11,11]', 159, 'Transfer till', '2016-04-25 08:04:45', 'Transfer till', 0, '0', '2016-04-25 08:20:45', '2016-04-25 08:20:45'),
(538, 66, '[7,11,11,11]', 160, 'Transfer till', '2016-04-25 09:04:04', 'Transfer till', 0, '0', '2016-04-25 09:06:04', '2016-04-25 09:06:04'),
(539, 66, '[7,11,11,11]', 1, 'Issue Till', '2016-04-26 07:04:29', 'Issue Till', 0, '0', '2016-04-26 07:35:29', '2016-04-26 07:35:29'),
(540, 66, '[7,11,11,11]', 162, 'Issue Till', '2016-04-26 07:04:32', 'Issue Till', 0, '0', '2016-04-26 07:38:32', '2016-04-26 07:38:32'),
(541, 69, '[7,11,11,11]', 163, 'return Till', '2016-04-26 08:04:33', 'return Till', 0, '0', '2016-04-26 08:39:33', '2016-04-26 08:39:33');

这是我的功能:

 public function getNotification($user_id =null, $gId=null)
    {
            if(is_array($gId)) {

                $this->_data = self::select('*')
                    ->join('users', 'users.id', '=', 'n_user_id')
                    ->where('users.id','=', $user_id)
                    ->whereIn('n_group_code_id',$gId)
                    ->get();
                if (count($this->_data)) {
                    return $this->_data;
                } else {
                    return false;
                }
            }else {
                return false;
            }

    }

最佳答案

这是可能的,但我建议您不要重新发明轮子并使用 json输入 DB 和 encode保存前的数组和 decode从数据库加载后。

首先,将您的列类型从字符串更改为 json(单击第一个链接以查找所有信息)。

保存数据:

$model = new Model();
....
$model->jsonData = json_encode($array);
$model->save();

检索数据:

$model = Model::find($id);
$array = json_decode($model->jsonData);

此外,您可以使用 accessors and mutators 自动执行此操作

关于php - 可以将数组值存储在数据库中并在 Laravel 中使用 where 子句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854561/

相关文章:

php - 函数参数太少,使用 Mail 时传入 0,而预期正好 1

laravel - 尝试让删除按钮显示图标

python - 使用 numpy.clip 将正值和负值转换为位串

c++ - 从指向数组的函数返回指针时出错

php - Laravel Eloquent 为帖子创建类别

php - filter_input() 何时删除 POST 变量的斜杠?

python - 在 Python 中查找数组中列表的元素(Numpy、Pytorch)

php - 如何在 IN 中正确使用 CASE WHEN/THEN/ELSE?

php - 如何在不在php中插入每个循环的情况下将数组插入数据库

php - 处理查询/多个查询的多个结果时的最佳实践?