foreach limim limop break blade laravel
我的表:'blog'
id | cat | title
---------------------------
1 | 3,4 | Post 01 X
2 | 1,2 | Post 02
3 | 4,2 | Post 03 X
4 | 3,2 | Post 04
5 | 1,1 | Post 05
6 | 3,1 | Post 06
7 | 3,2 | Post 07
8 | 3,4 | Post 08 X
9 | 1,4 | Post 09 X
10 | 2,4 | Post 10 X
11 | 4,6 | Post 11 X
12 | 4,7 | Post 12 X
13 | 4,1 | Post 13 X
14 | 7,4 | Post 14 X
15 | 4,2 | Post 15 X
我的路线:routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\CategoryController
Route::get('/cat',[CategoryController::class,'CAT']);
我的控制器:app/Http/Controllers/Frontend/CategoryController.php
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
class CategoryController extends Controller
{
public function CAT(){
$cats = DB::table('blog')->orderBy('id','DESC')->get();
$cat_select = '4';
$data = array( 'cats' => $cats, 'cat_select' => $cat_select, );
return view('frontend.category', $data );
}
}
我的刀片:resources/ views/frontend/category.blade.php
@foreach($cats->take(5) as $row)
@php
$data = $row->cat;
$sep_cat = explode(',' , $data);
@endphp
@foreach ($sep_cat as $cat)
@if ( $cat == $cat_select )
{{$row->id}} | {{$row->title}}
@endif
@endforeach
@endforeach
输出:
1 | Post 01
3 | Post 03
但我想显示输出如下:( $cat_select = '4' and Limit: 5 )
1 | Post 01
3 | Post 03
8 | Post 08
9 | Post 09
10 | Post 10
我检查了此链接如下但无法正确显示: 限制 Blade foreach 循环中的结果
如何更改代码?
My Table: 'blog'
id | cat | title
---------------------------
1 | 3,4 | Post 01 X
2 | 1,2 | Post 02
3 | 4,2 | Post 03 X
4 | 3,2 | Post 04
5 | 1,1 | Post 05
6 | 3,1 | Post 06
7 | 3,2 | Post 07
8 | 3,4 | Post 08 X
9 | 1,4 | Post 09 X
10 | 2,4 | Post 10 X
11 | 4,6 | Post 11 X
12 | 4,7 | Post 12 X
13 | 4,1 | Post 13 X
14 | 7,4 | Post 14 X
15 | 4,2 | Post 15 X
My Route: routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\CategoryController
Route::get('/cat',[CategoryController::class,'CAT']);
My Controller: app/Http/Controllers/Frontend/CategoryController.php
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
class CategoryController extends Controller
{
public function CAT(){
$cats = DB::table('blog')->orderBy('id','DESC')->get();
$cat_select = '4';
$data = array( 'cats' => $cats, 'cat_select' => $cat_select, );
return view('frontend.category', $data );
}
}
My Blade: resources/views/frontend/category.blade.php
@foreach($cats->take(5) as $row)
@php
$data = $row->cat;
$sep_cat = explode(',' , $data);
@endphp
@foreach ($sep_cat as $cat)
@if ( $cat == $cat_select )
{{$row->id}} | {{$row->title}}
@endif
@endforeach
@endforeach
Output:
1 | Post 01
3 | Post 03
But i want to display the output as follows: ( $cat_select = '4' and Limit: 5 )
1 | Post 01
3 | Post 03
8 | Post 08
9 | Post 09
10 | Post 10
I checked this link below but it does not display correctly:
Limiting the results in Blade foreach loop
How do I change the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而不是
foreach loop
使用laravel查询以cat_select = 4
使用whereraw
查询查询5记录,因此请减少代码并获得更快的输出。
Instead of
foreach loop
use laravel query to get 5 record withcat_select = 4
usingwhereRaw
queryso decrease the code and get faster output.
您的
$cats->take(5)
获取前 5 个结果(因此 id 1 到 5. 8、9 然后不属于该选择的一部分)。您可以使用随每个帖子更新的计数器,然后在 5 后中断:(我还清理了您的刀片)
Your
$cats->take(5)
takes the first 5 results (so id 1 to 5. 8, 9 and then aren't part of that selection). You can use a counter that you update with each post, and then break after 5:(I've also cleaned up your blade a bit)
最佳实践是首先根据您的条件设置查询。
做你的app/Http/Controllers/Frontend/CategoryController.php:
但我建议你在表中使用json:
所以你的查询将更改为:
resources/views/frontend/category.blade.php将是:
The best practice is set query with your condition first.
do your app/Http/Controllers/Frontend/CategoryController.php:
But I recommend you to use json in the table:
so your query will change to this:
and resources/views/frontend/category.blade.php will be: