PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

[FIXED] How to foreach categories from 3rd index from my database / Laravel?

 May 16, 2022     laravel, php     No comments   

Issue

I am issuing a problem with the following:

I foreach-ed my categories from index 1 to 3 in my blade file.

Code

@foreach($categories->take(3) as $category)
<ul>
<li>{{$category->name}}</li>
</ul>
@endforeach

But now I want to foreach second half of my categories from my database on the other ul list.

How to do that..?

Thanks:)


Solution

You can use splice method to popout the first 3 elements and then use another foreach to create another ul list from the other categories like below:

<?php

@foreach($categories->splice(0, 3) as $category)
<ul>
  <li><a>{{ $category->name }}</a>
</ul>
@endforeach

@foreach($categories as $category)
<ul>
  <li><a>{{ $category->name }}</a>
</ul>
@endforeach


Answered By - nice_dev
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing