我正在尝试从数据库中选择特定类别中最喜欢的图片。我有一个存储图片的数据库表和一个存储喜欢图片的表。图片表通过hasMany多态关系与可喜表相关。
喜欢模特:
public function likeable()
{
return $this->morphTo();
}
图片模型:
public function likes()
{
return $this->morphMany('App\Like', 'likeable');
}
家庭控制器:
Picture::where('picture_type', 'food')->orderBy(likes(), 'desc')->first();
喜欢的迁移:
public function up()
{
Schema::create('likeable', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('likeable_id');
$table->string('likeable_type');
$table->timestamps();
});
}
到目前为止,以上代码行在我的家庭控制器中返回错误。据我了解,likes方法必须在特定实例上调用。但是,我不知道在保留我的where子句的同时如何实现这一点。
谢谢您的帮助。
答案 0 :(得分:1)
我想您想按最喜欢的顺序订购图片。
在这种情况下,您需要这样的东西:
library(leaflet)
library(dplyr)
library(shiny)
library(echarts4r)
# Create 3 map points -- 2 will be clustered
map_points <- bind_rows(c(location = 'A', lon = -122.4, lat = 37.8),
c(location = 'B', lon = -122.4, lat = 37.8),
c(location = 'C', lon = -118.2, lat = 34.0))
map_points$lon <- as.numeric(map_points$lon)
map_points$lat <- as.numeric(map_points$lat)
# Shiny
ui <- fluidPage(
leafletOutput("mymap"),
echarts4rOutput("myplot")
)
server <- function(input, output, session){
output$mymap <- renderLeaflet({
leaflet(map_points) %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addCircleMarkers(lng = ~lon,
lat = ~lat,
group = "locations",
layerId = ~location,
# adding clusterOptions removes the group in observeEvent
clusterOptions = markerClusterOptions()
)
})
observeEvent(input$mymap_marker_click, {
print(input$mymap_marker_click)
})
output$myplot<-renderEcharts4r({
ex2<-map_points%>%
e_charts(x = lon)
ex2%>% e_scatter(lat)
})
}
shinyApp(ui = ui, server = server)
答案 1 :(得分:0)
只需使用您的表列名而不是likes
函数:
Picture::where('picture_type', 'food')->orderBy('likeable', 'desc')->first();