Laravel在查询构建器中使用UNION(Laravel using UNION in query builder)
我有一个SQL查询工作正常,我正在尝试转换为流利::
SELECT DISTINCT tags.tag FROM tags, items WHERE tags.taggable_type = 'Item' AND items.item_list_id = '1' UNION SELECT DISTINCT tags.tag FROM tags, itemlists WHERE tags.taggable_type = 'ItemList' AND itemlists.id = '1'
这是我到目前为止流利的,从文档中可以看出这一切似乎是正确的,并且各个查询都是自己工作的,只是当我联合它们时它会抛出一个错误:
$itemTags = Tag::join('items', 'items.id', '=', 'tags.taggable_id') ->select('tags.tag') ->distinct() ->where('tags.taggable_type', '=', 'Item') ->where('items.item_list_id', '=', $itemList->id); $itemListTags = Tag::join('itemlists', 'itemlists.id', '=', 'tags.taggable_id') ->select('tags.tag') ->distinct() ->where('tags.taggable_type', '=', 'ItemList') ->where('itemlists.id', '=', $itemList->id); // the var_dump below shows the expected results for the individual queries // var_dump($itemTags->lists('tag'), $itemListTags->lists('tag')); exit; return $itemTags ->union($itemListTags) ->get();
当我运行它时,我得到以下错误(我也在模型上从Ardent交换回Eloquent,以防有所作为 - 它没有):
Argument 1 passed to Illuminate\Database\Query\Builder::mergeBindings() must be an instance of Illuminate\Database\Query\Builder, instance of LaravelBook\Ardent\Builder given, called in path/to/root\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 898 and defined
I have an SQL query that works fine and I'm trying to convert into fluent::
SELECT DISTINCT tags.tag FROM tags, items WHERE tags.taggable_type = 'Item' AND items.item_list_id = '1' UNION SELECT DISTINCT tags.tag FROM tags, itemlists WHERE tags.taggable_type = 'ItemList' AND itemlists.id = '1'
This is what I have so far in fluent, it all seems right as far as I can tell from the docs and the individual queries both work on their own, it's just when I UNION them it throws an error:
$itemTags = Tag::join('items', 'items.id', '=', 'tags.taggable_id') ->select('tags.tag') ->distinct() ->where('tags.taggable_type', '=', 'Item') ->where('items.item_list_id', '=', $itemList->id); $itemListTags = Tag::join('itemlists', 'itemlists.id', '=', 'tags.taggable_id') ->select('tags.tag') ->distinct() ->where('tags.taggable_type', '=', 'ItemList') ->where('itemlists.id', '=', $itemList->id); // the var_dump below shows the expected results for the individual queries // var_dump($itemTags->lists('tag'), $itemListTags->lists('tag')); exit; return $itemTags ->union($itemListTags) ->get();
I get the following error when I run it (I've also swapped from Ardent back to Eloquent on the model in case that made a difference - it doesn't):
Argument 1 passed to Illuminate\Database\Query\Builder::mergeBindings() must be an instance of Illuminate\Database\Query\Builder, instance of LaravelBook\Ardent\Builder given, called in path/to/root\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 898 and defined
原文:https://stackoverflow.com/questions/19050387
最满意答案
如果您愿意放弃
knitr
组合生成LaTeX
格式的表,并使用pandoc
和rmarkdown
将其渲染为pdf。 这是一个简单的例子:sink("trial.Rmd") ```{r, echo = FALSE} exampleDF <- data.frame(a = 1:5, b = letters[1:5]) knitr::kable(exampleDF) ``` sink() rmarkdown::render("trial.Rmd", "pdf_document")
请注意,如上所述,此解决方案取决于安装了
pandoc
。 但它还取决于安装LaTeX
。 希望它有用。编辑:对不起,刚刚注意到你提到你不想使用markdown,但如果你愿意这样做,上面应该有用。
I found the answer somewhere else in stack :
writing data frame to pdf table
Adding text to a grid.table plot
Sorry for not having looked harder before !
相关问答
更多-
我知道的唯一方法就是用LaTeX,但你不一定需要Sweave; 也许你可以直接用你的RPython代码生成LaTeX文件。 如果您有两张名为tmp-001.pdf和tmp-002.pdf图片为6x6(默认大小),您将如何制作一个名为Section A的部分,其中有两个子部分用于放置两张图片。 \documentclass{article} \usepackage[paperwidth=6in, paperheight=6in, margin=0in]{geometry} \usepackage{hyperr ...
-
如何将多个seaborn图保存到同一个pdf / png文件中?(How to save multiple seaborn plots to the same pdf/png file?)[2022-06-13]
Seaborn的jointplot没有返回一个figure ,它返回一个seaborn.JointGrid实例。 你需要将一个数字传递给pdf_pages.savefig ,所以使用: with PdfPages('kde.pdf') as pdf_pages: for i in range(1, 114): for j in range(0, 113): x=t1.iloc[:, i] y=t1.iloc[:, j] ... -
如果您要在每个页面上绘制Subject的子集,那么您必须为每次迭代对数据进行子集化,然后绘制。 要在每个页面上获取4个主题,可以使用以下索引构建器作为子集的基础: (i - 1) * 4 + 1:4 Theoph数据集的技巧是主题“数字”实际上是有序因子。 因此,您必须将上述内容转换为一个因子,或者作为快捷方式转换为字符向量。 for(i in 1:3){ ## Changed mypath to make it reproducible mypath <- file.path(tempd ...
-
通过修复我的Rmarkdown模板,我能够解决我的问题: --- title: "Data Report" output: pdf_document: fig_caption: no subtitle: Quarter 4 Report --- ###This is just a sample of my code for the calculation Figure 1. Transfers to Hospital ```{r, echo=FALSE, fig.width=12, fig.height ...
-
使用layout()而不是split.screen()似乎是更好的选择。 也保持它在循环之外。 pdf('prova.pdf', width=14, height=7) layout(matrix(1:2, nrow=1)) for(i in totsites) { # the pdf should contain "totsites" number of pages (in this case, 4) if(i %in% sitesA) { plot(varA1[i,], ylim=c(0, ...
-
如果您愿意放弃pdf函数,可以使用knitr组合生成LaTeX格式的表,并使用pandoc和rmarkdown将其渲染为pdf。 这是一个简单的例子: sink("trial.Rmd") ```{r, echo = FALSE} exampleDF <- data.frame(a = 1:5, b = letters[1:5]) knitr::kable(exampleDF) ``` sink() rmarkdown::render("trial.Rmd", "pdf_document") 请注意,如上 ...
-
R - Mapply功能创建和PDF格式/ ggplots(R - Mapply Functionality Creating and PDFing Plots/ggplots)[2023-01-20]
让我重新格式化你的代码: library(ggplot2) library(gridExtra) TDSF <- data.frame(Graduation=sample(1950:2010, 30,replace=TRUE), Donation=sample(10:50000, 30,replace=TRUE), Start.Year=sample(1950:2010,30,replace=TRUE), ... -
在要导出为pdf的笔记本中评估此值: Export["~/Desktop/nb.pdf", SelectedNotebook[]] Evaluate this in the notebook you want to export as pdf: Export["~/Desktop/nb.pdf", SelectedNotebook[]]
-
R - 将页码添加到PDF(R - adding page numbers to PDF)[2022-08-27]
从我对Q的第二个评论中,我提出了一个使用mtext()的基本图形解决方案。 这似乎适用于OP,所以我在这里展示了一个扩展版本: 基础图形: op <- par(oma = c(2,0,0,0)) layout(matrix(1:4, ncol = 2)) plot(1:10) plot(1:10) plot(1:10) plot(1:10) mtext(side = 1, text = "Page 1", outer = TRUE) layout(1) par(op) 导致: @ SFun28报道这个想法 ... -
R中的1页上有多个图(Multiple Plots on 1 page in R)[2022-03-16]
要创建绘图的pdf,您可以执行类似的操作。 要初始化pdf文档,请首先使用带有文件名的pdf函数。 dev.off()最后将关闭图形设备并完成pdf。 之后,您应该在工作目录中看到一个新文档(在此示例中为'plots.pdf')。 d <- data.frame(matrix(sample(c(1:700), 2000, TRUE), 10, 20)) pdf('plots') par(mfrow = c(3, 3)) ## set the layout to be 3 by 3 sapply(1:9, ...