控制器:
$view_log = DB::table('view')->where('user_id', '!=', null)
->orderby('created_at', 'desc')
->take(5)
->get();
查看:
@foreach ($view_log as $view)
{{ $view->user_id }} viewed {{ $view->item_id }} {{ $view->created_at->diffForHumans() }}<br />
@endforeach
查看表格结构:
id | item_id | user_id | created_at
1 | 930 | 3 |
2 | 930 | null |
3 | 837 | 5 |
4 | 378 | null |
null =来宾,user_id =经过身份验证的用户
预期产出:
3 viewed 930 2 minutes ago
3 viewed 922 3 minutes ago
5 viewed 837 7 minutes ago
2 viewed 888 9 minutes ago
1 viewed 930 9 minutes ago
错误:
InvalidArgumentException
Value must be provided.
我认为它与:
有关->where('user_id', '!=', null)
如何只选择表格中包含user_id
?
答案 0 :(得分:2)
试试这个:
DB::table('table')->whereNotNull('column')->get();
<强>来源:强> http://laravel.com/docs/queries#selects
同样在SQL中,!= null
应该是IS NOT NULL
所以->where('user_id', 'IS NOT', null)
也应该有用
答案 1 :(得分:1)
答案 2 :(得分:1)
否'!='
查询必须为WHERE user_id IS NOT NULL
,因此我认为您必须使用'!='
更改IS NOT