如何在Laravel查询中执行IS NOT NULL

时间:2016-12-23 11:32:08

标签: php mysql laravel blade

我试图执行此SQL语句并使用刀片引擎输出它:

" SELECT SUM(like) FROM likes WHERE post_id = ". $post->id. " AND source_id IS NOT '' "

我不知道该怎么做,我昨天试了这个,但它没有用:

{{$post->likes->where('post_id', $post->id)->where('source_id', '<>', '')->sum('like')}}

喜欢表&#34;属于&#34; 来源表和帖子表。

修改

我使用的是Laravel版 5.2.45

3 个答案:

答案 0 :(得分:2)

您可以使用whereNotNull()方法。

https://laravel.com/docs/5.3/queries#where-clauses

<强>更新

似乎Target "MyLib " links to target "Qt5::XXX" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? 是一个集合。在这种情况下,您必须使用filter()。例如:

$posts

答案 1 :(得分:0)

你有没有试过这个......

{{$post->likes->where('post_id', $post->id)->whereNotNull('source_id')->sum('like')}}

也可以尝试

{{$post->likes->
where([
    ['post_id', '=', $post_id],
    ['source_id', '<>', NULL],])
->sum('like') }}

答案 2 :(得分:0)

对于Laravel 4./5。它的whereNotNull()和Laravel 3:它的where_not_null()。

因此,如果版本是Laravel 4./5。,

,那么您的查询将是这样
/*Portrait phones */
@media (max-width:320px){}

/* Landscape phones and down */
@media (min-width: 321px) and (max-width: 480px) {}

如果版本是Laravel 3,

将是此查询
{{$post->likes->where('post_id', $post->id)->whereNotNull('source_id')->sum('like')}}