foreach limim limop break blade laravel

发布于 2025-01-19 09:03:31 字数 2146 浏览 0 评论 0原文

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

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

发布评论

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

评论(3

国产ˉ祖宗 2025-01-26 09:03:31

而不是foreach loop使用laravel查询以cat_select = 4使用whereraw查询查询5记录,

$cats = DB::table('blog')->whereRaw("find_in_set('4',cat)")->limit(5)->get();

@foreach($cats as $row)
    {{$row->id}} |  {{$row->title}}    
@endforeach

因此请减少代码并获得更快的输出。

Instead of foreach loop use laravel query to get 5 record with cat_select = 4 using whereRaw query

$cats = DB::table('blog')->whereRaw("find_in_set('4',cat)")->limit(5)->get();

@foreach($cats as $row)
    {{$row->id}} |  {{$row->title}}    
@endforeach

so decrease the code and get faster output.

青衫负雪 2025-01-26 09:03:31

您的 $cats->take(5) 获取前 5 个结果(因此 id 1 到 5. 8、9 然后不属于该选择的一部分)。您可以使用随每个帖子更新的计数器,然后在 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

我还清理了您的刀片)

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:

@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

(I've also cleaned up your blade a bit)

零度℉ 2025-01-26 09:03:31

最佳实践是首先根据您的条件设置查询。
做你的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 );
    }
}

但我建议你在表中使用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

The best practice is set query with your condition first.
do your 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 );
    }
}

But I recommend you to use json in the table:

  id  |   cat  |  title
---------------------------
   1  |   [3,4]  |  Post 01    X
   2  |   [1,2]  |  Post 02
   3  |   [4,2]  |  Post 03    X

so your query will change to this:

        $cats = DB::table('blog')->whereJsonContains('cat', $cat_select)
                 ->orderBy('id','DESC')->get();

and resources/views/frontend/category.blade.php will be:

@foreach($cats as $row)
    {{$row->id}} |  {{$row->title}}
@endforeach
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文