Home > Mobile >  foreach limit loop break blade laravel
foreach limit loop break blade laravel

Time:04-06

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?

CodePudding user response:

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)

CodePudding user response:

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
  • Related