我的表:'home'
id | selected
-------------------
1 | 3
2 | 5
3 | 6
4 | 7
5 | 9
我的表:"博客">
id | cat | title
---------------------------
1 | 3,4 | Post 01
2 | 1,2 | Post 02
3 | 4,2 | Post 03
4 | 3,2 | Post 04
5 | 1,1 | Post 05
6 | 3,1 | Post 06
7 | 3,2 | Post 07
8 | 3,5 | Post 08
9 | 1,4 | Post 09
10 | 2,4 | Post 10
11 | 4,6 | Post 11
12 | 9,7 | Post 12
13 | 4,3 | Post 13
14 | 7,4 | Post 14
15 | 4,3 | Post 15
我的路线:routes/web.php
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersFrontendHomeController
Route::get('/home',[CategoryController::class,'Home']);
我的控制器:app/Http/Controllers/Fronend/HomeController.php
<?php
namespace AppHttpControllersFrontend;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesRoute;
class CategoryController extends Controller
{
public function Home(){
$home = DB::table('home')->whereRaw("find_in_set(?,selected)")->limit(4)->get();
$blog = DB::table('blog')->limit(3)->get();
$data = array( 'home' => $home, 'blog' => $blog );
return view('frontend.home', $data );
}
}
我的刀片:resources/views/frontend/category.Blade.php
@foreach($home as $row_home)
{{$row_home->id}} |
@php $i = 0; $selected = $row_home->selected; @endphp
@foreach($blog as $row_blog)
@if (in_array( $selected , explode(',' , $row_blog->cat)))
{{$row_blog->title}} ,
@php $i++; @endphp
@if($i == 5) @break @endif
@endif
@endforeach
@endforeach
输出:
3 | Post 01 ,
5 |
6 |
7 |
但我想显示如下输出:
3 | Post 01 , Post 04 , Post 06
5 | Post 08 ,
6 | Post 11 ,
7 | Post 12 , Post 14
我检查了下面的链接,但它显示不正确:前限位环路断路刀片左
如何更改代码?
@foreach($home as $row_home)
{{$row_home->id}}
@php $i = 0; $selected = $row_home->selected; @endphp
@foreach($blog as $row_blog)
@if (in_array( $selected , explode(';' , $row_blog->cat)))
{{$row_blog->title}} ,
@php $i++; @endphp
@if($i == 5) @break @endif
@break
@endif
@endforeach
@endforeach