Why do you want to avoid foreach loop?
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) }}
i have to dipaly data in booking table where in m using @for($td = 0; $td < 7 ; $td++) soit display data 7 time
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>
--}}
// 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
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()
u can use array_slice
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 ...!!
How can i escape foreach loop ....??
u can use array_slice
go back and put three backticks ``` before and after code blocks. Then more people will look at your question
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'));
}
'''
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>
'''
i this above view how can i escape ''' @foreach( $bookings as $book)''' llop
Can i use like this inside
''' @foreach( $bookings as $book)
{{-- some code--}}
{{ ucfirst($book->name)}}
to this
$book = DB::table('bookings') ->orderBy('employee', 'asc')->get();
{{ ucfirst($book->name)}}
'''
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.