Laravel增加了一个多态关系列

时间:2016-01-19 12:13:41

标签: laravel laravel-5 eloquent polymorphic-associations

我目前有一个像这样设置的监听器,无论何时从控制器提供页面,都会触发事件:

public function handle(BarPageWasViewed $event)
{
    // increment the pageview for this particular bar whenever the event fires
    $event->bar->views()->create(['views' => 1]);
}

这会插入多态关系并将相应的views列设置为1。

现在只要这种关系已经存在,我只想增加视图。我该怎么做呢?我通过的$ event只是' bar'目前正在浏览的模型。

1 个答案:

答案 0 :(得分:0)

想出来,对于那些处理同样问题的人来说:

In [145]: %timeit df.name.value_counts().argmax()
The slowest run took 5.25 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 322 µs per loop

In [146]: %timeit df.name.value_counts().index[0]
The slowest run took 6.32 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 275 µs per loop

In [147]: %timeit df.name.value_counts().first_valid_index()
The slowest run took 5.43 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 292 µs per loop

In [148]: %timeit df.name.value_counts().idxmax()
The slowest run took 5.30 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 321 µs per loop