我正在使用Laravel 5,当我有斜线(向前或向后)时我遇到查询问题
出版物表格结构:
CREATE TABLE IF NOT EXISTS `publications` (
`id` bigint(20) NOT NULL,
`user_id` bigint(20) NOT NULL,
`title` varchar(512) NOT NULL,
`type` varchar(512) NOT NULL,
`company` varchar(512) NOT NULL,
`author` varchar(512) NOT NULL,
`firm` varchar(512) NOT NULL,
`address` text NOT NULL,
`sector_id` bigint(20) NOT NULL DEFAULT '0',
`date_from` timestamp NULL DEFAULT NULL,
`date_to` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`region_id` bigint(20) NOT NULL DEFAULT '0',
`file_name` varchar(512) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` bigint(20) NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_by` bigint(20) NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`category_id` bigint(20) NOT NULL,
`sub_category_id` bigint(20) NOT NULL,
`manually_verified` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
数据:
INSERT INTO `publications` (`id`, `user_id`, `title`, `type`, `company`, `author`, `firm`, `address`, `sector_id`, `date_from`, `date_to`, `region_id`, `file_name`, `created_at`, `created_by`, `updated_at`, `updated_by`, `is_active`, `category_id`, `sub_category_id`, `manually_verified`) VALUES
(3, 7, 'test publication 3', 'test 1', 'test 1', 'test author 1', 'test 1', 'test3', 1, '2015-06-09 11:54:18', '2015-06-29 18:30:00', 0, 'files/testing/testing.pdf', '2015-06-06 05:58:10', 7, '2015-06-13 03:31:45', 7, 0, 4, 1, 0),
(4, 7, 'test publication 4', 'test 2', 'test 2', 'test author 2', 'test 2', 'test2', 1, '2015-06-09 11:54:23', '2015-06-29 18:30:00', 0, 'test1.pdf', '2015-06-06 05:58:10', 7, '2015-06-05 23:58:10', 7, 1, 4, 1, 0),
(5, 7, 'test 5', 'test', 'test', 'test', 'test', 'test', 1, '2015-06-09 11:54:09', '2015-06-29 18:30:00', 0, 'files/Bugzilla-uermanual.pdf', '2015-06-06 05:58:10', 7, '2015-06-13 03:19:21', 7, 0, 4, 1, 1);
Laravel 5
$arr = array( 'file_paths' => Array ( 'files/testing/testing.pdf', 'files/Bugzilla-uermanual.pdf' ) );
Query
$this->publications
->where(['category_id'=>$search_cat_id])
->where(function ($query) use ($arr){
foreach ($arr['file_paths'] as $key=>$value){
$query->orWhere('file_name', DB::connection()->getPdo()->quote($value));
}
})
->get();
print_r($publications_records);
我没有得到任何结果。如果我执行laravel生成的查询,它就会被执行。
以下是Laravel生成的查询,如果我在MySQL中执行此操作,我会得到结果。
select * from `publications`
where (`category_id` = 4) and
(`file_name` = 'files/testing/testing.pdf'
or `file_name` = 'files/Bugzilla-uermanual.pdf')
order by `id` desc
答案 0 :(得分:0)
试试这个:
$arr = ['files/testing/testing.pdf', 'files/Bugzilla-uermanual.pdf'];
$publications_records = $this->publications()
->where('category_id', $search_cat_id)
->where(function ($query) use ($arr){
foreach ($arr as $value){
$query->orWhere('file_name', $value);
}
})
->get();
dd($publications_records, DB:getQueryLog());
如果上面的代码不起作用。请编辑您的问题并粘贴输出