我有 3 个相关的模型,如下所示:
Prize -> TimeSlot -> Winner
,它们的对应关系方法$prize->hasMany(TimeSlot)
和$prize->hasManyThrough(Winner, TimeSlot)
.
我试图在一个雄辩的查询中计算获胜者的数量和时间段的数量。
这是查询:
$prizes = $prizes->withCount(['winners', 'timeSlots' => function ($query) {
$query->sum('min'); // min is an attribute on TimeSlot
}])->get();
Run Code Online (Sandbox Code Playgroud)
这为我提供了winners_count
每个奖项具有属性的获奖者数量。我以为它会给我每个奖品的时间段的“总和数”,但查询在该部分中断并出现如下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prizes.id' in 'where clause'
(SQL: select sum(`min`) as aggregate from `time_slots` where `time_slots`.`prize_id` = `prizes`.`id`)
Run Code Online (Sandbox Code Playgroud)
withCount
方法的文档仅显示如何约束查询(例如使用 where 子句),而不是使用 sum 之类的聚合。
PS:我已经知道如何使用以下方法执行此操作:
$prizes = $prizes->with('timeSlots')->withCount('winners')->get();
$numOfWinners = $prizes->sum('winners_count');
$numOfAvailablePrizes = $prizes->sum(function ($prize) {
return $prize->timeSlots()->sum('min');
});
Run Code Online (Sandbox Code Playgroud)
但我认为带有一个查询的版本会为我节省一些无用的循环...... …
任何人都可以提供任何库以在 React Native 中显示和搜索 PDF 或 doc 文件吗?.
我已经测试了 WebView ,react-native-pdf-lib,react-native-pdf-view,react-native-doc-viewer库
但我无法搜索任何文件
谢谢你的帮助
我有三个型号Period
,,Post
:User
period
并且posts
有 M:N 关系period_posts
posts
并且user
有 M:N 关系views
时期型号:
class Period extends Model
{
public $timestamps=false;
public function posts()
{
return $this->belongsToMany(Post::class);
}
}
Run Code Online (Sandbox Code Playgroud)
帖子型号:
class Post extends Model
{
public function periods()
{
return $this->belongsToMany(Period::class);
}
public function views()
{
return $this->belongsToMany(User::class, 'views')
->withTimestamps();
}
}
Run Code Online (Sandbox Code Playgroud)
用户型号:
class User extends Authenticatable
{
use Notifiable;
public function views()
{
return $this->belongsToMany(Post::class, 'views')
->withTimestamps();
}
}
Run Code Online (Sandbox Code Playgroud)
视图表:
id
user_id …