下面的方法是取出一个用户的所有文章,再把文章分成两组,published
和unpublished
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <code> public function index(Request $request )
{
$articles = $request ->user()->articles()->get();
$published = $articles ->filter( function ( $article ) {
return $article ->status == 1;
});
$unpublished = $article ->filter( function ( $article ) {
return $article ->status == 0;
});
return view( 'user.dashboard.index' , compact( 'published' , 'unpublished' ));
}</code>
|
Nach dem Login kopieren
Nach dem Login kopieren
现在,在分组的时候,需要多增加一个过滤条件,比如这个:
1 2 3 | <code> $published = $articles ->filter( function ( $article ) {
return $article ->status == 1;
});</code>
|
Nach dem Login kopieren
Nach dem Login kopieren
上面的过滤的条件只有1个,就是status == 1
,
现在增加一个过滤条件publishing_time <br>那么,增加过滤条件后,语法怎么写呢?
回复内容:
下面的方法是取出一个用户的所有文章,再把文章分成两组,published
和unpublished
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <code> public function index(Request $request )
{
$articles = $request ->user()->articles()->get();
$published = $articles ->filter( function ( $article ) {
return $article ->status == 1;
});
$unpublished = $article ->filter( function ( $article ) {
return $article ->status == 0;
});
return view( 'user.dashboard.index' , compact( 'published' , 'unpublished' ));
}</code>
|
Nach dem Login kopieren
Nach dem Login kopieren
现在,在分组的时候,需要多增加一个过滤条件,比如这个:
1 2 3 | <code> $published = $articles ->filter( function ( $article ) {
return $article ->status == 1;
});</code>
|
Nach dem Login kopieren
Nach dem Login kopieren
上面的过滤的条件只有1个,就是status == 1
,
现在增加一个过滤条件publishing_time <br>那么,增加过滤条件后,语法怎么写呢?
第一,和你的问题无关,建议你修改下边这一行:
1 2 | <code> $articles = $request ->user()->articles()->get();
$articles = $request ->user()->articles
|
Nach dem Login kopieren
第二,来回答你的问题(你可能把问题想复杂了,其实像下边这样简单):
1 2 | <code> $published = $articles ->filter( function ( $article ) {
return $article ->status == 1 && $article ->publishing_time </code>
|
Nach dem Login kopieren