foreach 限制循环中断 blade laravel
foreach limit loop break blade laravel
我的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
我的路线: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 );
}
}
我的 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
输出:
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
我检查了下面这个 link 但它没有正确显示:
如何更改代码?
您的 $cats->take(5)
获取前 5 个结果(因此 id 1 到 5. 8、9 然后不属于该选择的一部分)。您可以使用每个 post 更新的计数器,然后在 5:
之后中断
@php $i = 0; @endphp
@foreach($cats as $row)
@if (in_array($cat_select, explode(',' , $row->cat)))
{{$row->id}} | {{$row->title}}
@php $i++; @endphp
@if($i == 5) @break @endif
@endif
@endforeach
(我也稍微清理了你的 blade)
最佳做法是先根据您的条件设置查询。
做你的 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(){
$cat_select = '4';
$cats = DB::table('blog')->where('cat', 'like', "$cat_select,%")
->orWhere('cat', 'like', "%,$cat_select")
->orWhere('cat', 'like', "%,$cat_select,%")
->orderBy('id','DESC')->get();
$data = array( 'cats' => $cats, 'cat_select' => $cat_select, );
return view('frontend.category', $data );
}
}
但我建议您在 table:
中使用 json
id | cat | title
---------------------------
1 | [3,4] | Post 01 X
2 | [1,2] | Post 02
3 | [4,2] | Post 03 X
因此您的查询将更改为:
$cats = DB::table('blog')->whereJsonContains('cat', $cat_select)
->orderBy('id','DESC')->get();
和 resources/views/frontend/category.blade.php 将是:
@foreach($cats as $row)
{{$row->id}} | {{$row->title}}
@endforeach
而不是 foreach loop
使用 laravel 查询获取 5 条记录 cat_select = 4
使用 whereRaw
查询
$cats = DB::table('blog')->whereRaw("find_in_set('4',cat)")->limit(5)->get();
@foreach($cats as $row)
{{$row->id}} | {{$row->title}}
@endforeach
所以减少代码并获得更快的输出。
我的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
我的路线: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 );
}
}
我的 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
输出:
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
我检查了下面这个 link 但它没有正确显示:
如何更改代码?
您的 $cats->take(5)
获取前 5 个结果(因此 id 1 到 5. 8、9 然后不属于该选择的一部分)。您可以使用每个 post 更新的计数器,然后在 5:
@php $i = 0; @endphp
@foreach($cats as $row)
@if (in_array($cat_select, explode(',' , $row->cat)))
{{$row->id}} | {{$row->title}}
@php $i++; @endphp
@if($i == 5) @break @endif
@endif
@endforeach
(我也稍微清理了你的 blade)
最佳做法是先根据您的条件设置查询。 做你的 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(){
$cat_select = '4';
$cats = DB::table('blog')->where('cat', 'like', "$cat_select,%")
->orWhere('cat', 'like', "%,$cat_select")
->orWhere('cat', 'like', "%,$cat_select,%")
->orderBy('id','DESC')->get();
$data = array( 'cats' => $cats, 'cat_select' => $cat_select, );
return view('frontend.category', $data );
}
}
但我建议您在 table:
中使用 json id | cat | title
---------------------------
1 | [3,4] | Post 01 X
2 | [1,2] | Post 02
3 | [4,2] | Post 03 X
因此您的查询将更改为:
$cats = DB::table('blog')->whereJsonContains('cat', $cat_select)
->orderBy('id','DESC')->get();
和 resources/views/frontend/category.blade.php 将是:
@foreach($cats as $row)
{{$row->id}} | {{$row->title}}
@endforeach
而不是 foreach loop
使用 laravel 查询获取 5 条记录 cat_select = 4
使用 whereRaw
查询
$cats = DB::table('blog')->whereRaw("find_in_set('4',cat)")->limit(5)->get();
@foreach($cats as $row)
{{$row->id}} | {{$row->title}}
@endforeach
所以减少代码并获得更快的输出。