Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

prashantgaurav1994's avatar

how can i avoid foreach loop

My controller

public function index( Request $request) { $bookings =DB::table("bookings")->whereRaw('date(created_at) = ?', [Carbon::today()])->orderBy('time')->get();

    $servicess = DB::table("branch__services") ->orderBy('created_at')->lists("service_id","id");//staff table for imployee
    
    $employees = DB::table("employee_to_branchs") ->orderBy('employee', 'asc')->get();
    return view('admin.bookings.index', compact('bookings','servicess','employees'));
}

my view

@foreach($bookings as $b)

Name : {{ ucfirst($b->name)}}
    Service : {{ ucfirst($b->service) }}

@endforeach

0 likes
15 replies
prashantgaurav1994's avatar

i have to dipaly data in booking table where in m using @for($td = 0; $td < 7 ; $td++) soit display data 7 time

prashantgaurav1994's avatar

This in my table body

{{--

@foreach ($employees as $emp) {{--emp is var--}} {{ucfirst($emp->employee)}} A/B @foreach($bookings as $b) @for($td = 0; $td < 7 ; $td++) @if($b->name != null) @if($emp->employee == $b->employees) @if($emp->branch == "Test Branch") @if($b->time == "09:00:00") Name: {{ ucfirst($b->name)}} || Service: {{ ucfirst($b->service) }} time: {{$b->time}} @endif @endif @else Book Now! @endif @endif @endfor @endforeach {{--@endforeach--}}
  @endforeach<!--branch-->
   </tbody>

--}}

prashantgaurav1994's avatar

// this in my table body

tbody> @foreach ($employees as $emp) {{--emp is var--}} {{ucfirst($emp->employee)}} A/B @foreach($bookings as $b) @for($td = 0; $td < 7 ; $td++) @if($b->name != null) @if($emp->employee == $b->employees) @if($emp->branch == "Test Branch") @if($b->time == "09:00:00") <b class="Display-dat"a style="color:#3366ff">Name: {{ ucfirst($b->name)}} || Service: {{ ucfirst($b->service) }} time: {{$b->time}} @endif @endif @else <i class=" fa fa-plus-square-o fa-2x" id="add" data-toggle="popover" data-html="true" data-title="More" data-container="body"style="color:#00cc00"> Book Now! @endif @endif @endfor @endforeach {{--@endforeach--}}

  @endforeach<!--branch-->
   </tbody
KenoKokoro's avatar

If you need to show the data 7 times, just add on your query limit.

$bookings =DB::table("bookings")->whereRaw('date(created_at) = ?', [Carbon::today()])->orderBy('time')->take(7)->get()

1 like
prashantgaurav1994's avatar

Actually One data is repeating at seven time that is the problem .......... due to foreach loop , how to solve that..??How Can i display data without using foreach loop....??Any idea ...!!

Snapey's avatar

go back and put three backticks ``` before and after code blocks. Then more people will look at your question

prashantgaurav1994's avatar

My Controller'''

public function index( Request $request)
{
    $bookings =DB::table("bookings")
    ->whereRaw('date(created_at) = ?', [Carbon::today()])
    ->orderBy('time')->get();
    // select name,services,time from bookings where employees=s and date=CURRENT_DATE
    $servicess = DB::table("branch__services") ->orderBy('created_at')->lists("service_id","id");//staff table for imployee
    // $branchss = DB::table("employee_to_branchs") ->orderBy('created_at')->lists("branch","id");
    $employees = DB::table("employee_to_branchs") ->orderBy('employee', 'asc')->get();
    // ->orderBy('employee', 'asc')
    // ->lists("employee","id");
    // select  employees from employee_to_branchs
    return view('admin.bookings.index', compact('bookings','servicess','employees'));
}

'''

prashantgaurav1994's avatar

And my view is ''' @foreach ($employees as $emp) {{--emp is var--}} {{ucfirst($emp->employee)}} A/B

          @foreach( $bookings as $book)


        @for($td = 0; $td < 7; $td++)
            @if($book->name != null) <!-- ck if ob j != null -->
              @if($emp->employee == $book->employees) <!-- comp employees row with db employees data -->
               @if($emp->branch == "Test Branch")
                @if($book->time == "09:00:00")<!--comp time  -->
                <td>
                 <!-- data Display code -->

                      <b class="Display-dat"a style="color:#3366ff">Name:</b><sapn style="color:#99ccff"> {{ ucfirst($book->name)}}</sapn> <span style="color:red">||</span>
                      <b style="color:#3366ff">Service:</b><sapn style="color:#99ccff"> {{ ucfirst($book->service) }}</sapn>
                      <b style="color:#3366ff">time:</b><sapn style="color:#99ccff"> {{$book->time}}</sapn>
                 @endif<!--time  -->
               @endif<!--branch cheacking-->
                  @else
                 <div align="right"> <!-- extra button -->
                   <a>
                     <i class=" fa fa-plus-square-o fa-2x" id="add" data-toggle="popover" data-html="true" data-title="More"  data-container="body"style="color:#00cc00"></i>
                   </a>
                </div>
               <br><br><b>Book Now!</b><br><br><br><br>
               <div align="right"> <!--  add button -->
                 <a>
                   <i class="Service-Data fa fa-user-plus fa-2x" style="color:#00cc00" data-id="{{$emp->employee}}" data-toggle="modal" data-target="#BookingModel" href="#BookingModel"></i>
                </a>
              </div>
            @endif<!--emp  -->
            @endif<!--  not null-->
          </td>
          @endfor
          @endforeach<!--booking-->
      </td>
      </tr>
    @endforeach<!--employees  -->


   </tbody>

'''

prashantgaurav1994's avatar

Can i use like this inside

''' @foreach( $bookings as $book)

{{-- some code--}}
   {{ ucfirst($book->name)}}

@endforeach

to this

$book = DB::table('bookings') ->orderBy('employee', 'asc')->get();

{{ ucfirst($book->name)}}

'''

Snapey's avatar

back tick before and after your code not single quote ```

What you are trying to do is really bad practice and you have not explained why you want to do it.

You can ask the same question three times but the answer is the same - don't do it.

Do all your queries in the controller

foreach in your view to iterate over any number of rows according to the size of the collection that is returned.

This is really basic

Please or to participate in this conversation.