限制find_in_set wheraw foreach laravel
我的表:'home'
id | selected
-------------------
1 | 3
2 | 5
3 | 6
4 | 7
5 | 9
我的表:'blog'
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 Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\HomeController
Route::get('/home',[CategoryController::class,'Home']);
我的控制器:app/Http/Controllers/Frontend/HomeController.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 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 限制循环中断blade laravel
如何更改代码?
My Table: 'home'
id | selected
-------------------
1 | 3
2 | 5
3 | 6
4 | 7
5 | 9
My Table: 'blog'
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
My Route: routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\HomeController
Route::get('/home',[CategoryController::class,'Home']);
My Controller: app/Http/Controllers/Frontend/HomeController.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 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 );
}
}
My Blade: 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
Output:
3 | Post 01 ,
5 |
6 |
7 |
But i want to display the output as follows:
3 | Post 01 , Post 04 , Post 06
5 | Post 08 ,
6 | Post 11 ,
7 | Post 12 , Post 14
I checked this link below but it does not display correctly:
foreach limit loop break blade laravel
How do I change the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)