限制find_in_set wheraw foreach laravel

发布于 2025-01-19 20:08:11 字数 2345 浏览 0 评论 0原文

我的表:'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2025-01-26 20:08:11
@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
@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文