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

时间:2021-11-10 11:48:01

I want to store array type in a column in database and select it back using where to compare n_group_code_id between array($gID) as below function.

我想将数组类型存储在数据库的一个列中,然后使用where将数组($ 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');

Here is my function:

这是我的功能:

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

    }

1 个解决方案

#1


6  

It's possible, but I'd recommend you to not reinvent the wheel and use json type in DB and encode your array before saving and decode it after loading from DB.

这是可能的,但我建议你不要重新发明*并在数据库中使用json类型并对数据进行编码,然后在从DB加载后进行保存和解码。

First of all, change your column type from string to json (click first link to find all info).

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

To save data:

要保存数据:

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

To retrieve data:

要检索数据:

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

Also, you could do that automatically with using accessors and mutators

此外,您可以使用访问器和更改器自动执行此操作

#1


6  

It's possible, but I'd recommend you to not reinvent the wheel and use json type in DB and encode your array before saving and decode it after loading from DB.

这是可能的,但我建议你不要重新发明*并在数据库中使用json类型并对数据进行编码,然后在从DB加载后进行保存和解码。

First of all, change your column type from string to json (click first link to find all info).

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

To save data:

要保存数据:

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

To retrieve data:

要检索数据:

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

Also, you could do that automatically with using accessors and mutators

此外,您可以使用访问器和更改器自动执行此操作