it loops again after if else condition - laravel-5

i have made a loop to increment the date but then it loops as many times as the if condition becomes true.
#foreach($c as $cc)
#for($i = $range_from; $i <= $range_to; $i++)
<div class="text-center">
{{ date('m-d-y', strtotime($i)) }}
<div style="margin-bottom: 20px;">
#if( $cc->attendance_date == date('Y-m-d', strtotime($i)) )
<br><h1>present</h1><br>
#else
<br>absent<br>
#endif
</div>
</div>
#endfor
#endforeach

Related

Getting the sum of two multiplied index of two different dynamic input field - Laravel Livewire

Problem
I want to get the sum of all ingredients and multiply it from the measurement given by the user, these are the list of measurements and their respective values (in number),
-pcs (100)
-mL (1)
-cups (183)
-liter (1000)
-kgs (1000)
-grams (1)
and it must give the automated value
this is a sample input
multiple the first row (in my case 1 * pcs (with the value of 100))
Then after multiplying the rows with the measurements
The Serving - autoCalculate must have the value of ($total / 300);
My Livewire Component
'<?php
namespace App\Http\Livewire;
use App\Models\Recipe;
use Livewire\Component;
class MainIngredient extends Component
{
public $main_ingredientsRecipeName = [];
public $main_ingredientsNumberPortion = [];
public $main_ingredientsRecipeMeasure = [];
public $allRecipes = [];
public $total_numberPortion = 0;
public $sum_numberPortion = 0;
public $total_measurement = 0;
public $sum_measurement = 0;
public $numberPortion;
public function mount()
{
$this->allRecipes = Recipe::all();
$this->main_ingredientsNumberPortion = [
[
'numberPortion' => '',
$this->total_numberPortion += 1,
],
];
$this->main_ingredientsRecipeMeasure = [
[
'measurement' => '',
$this->total_measurement += 1,
],
];
$this->main_ingredientsRecipeName = [
['main_ingredient' => '',]
];
}
public function addMainIngredient()
{
$this->main_ingredientsNumberPortion[] =
[
'numberPortion' => '',
$this->total_numberPortion += 1,
];
$this->main_ingredientsRecipeMeasure[] =
[
'measurement' => '',
$this->total_measurement += 1,
];
$this->main_ingredientsRecipeName[] =
[
'main_ingredient' => '',
];
}
public function removeMainIngredient($index)
{
unset($this->main_ingredientsNumberPortion[$index]);
unset($this->main_ingredientsRecipeMeasure[$index]);
unset($this->main_ingredientsRecipeName[$index]);
$this->main_ingredientsNumberPortion = array_values(
$this->main_ingredientsNumberPortion
);
$this->main_ingredientsRecipeMeasure = array_values(
$this->main_ingredientsRecipeMeasure
);
$this->main_ingredientsRecipeName = array_values(
$this->main_ingredientsRecipeName
);
}
public function render()
{
// dd($this->total_numberPortion);
return view('livewire.main-ingredient');
}
}'
Laravel View
<div style="background-color:#509941;" class="rounded p-3 w-50 m-auto ">
<div style="display: none">
{{ $total = 0 }}
</div>
<h5 style="font-weight: 500" class="text-start text-light py-2">Ingredients with portions:
</h5>
<div class="row d-flex justify-content-between ">
<div class="col-lg-2 my-auto">
#foreach ($main_ingredientsNumberPortion as $index => $numberPortion)
<input class="my-1 form-control " style="border: none; outline:none;"
type="number"
id="numberPortion-checker" name="numberPortion[{{ $index }}]"
wire:model="main_ingredientsNumberPortion.{{ $index }}.numberPortion"
required>
#endforeach
</div>
<div class="col-lg-2 my-auto">
#foreach ($main_ingredientsRecipeMeasure as $index => $measurement)
<select style="color: rgb(17, 80, 17); border: none; outline:none;"
id="measurement-checker"
class="my-1 typeahed py-1 px-1 rounded" name="measurement[{{ $index }}]"
wire:model="main_ingredientsRecipeMeasure.{{ $index }}.measurement"
value="pcs" required>
<option value="pcs">pcs</option>
<option value="mL">mL</option>
<option value="cups">cups</option>
<option value="liter">liter</option>
<option value="kgs">kgs</option>
<option value="grams">grams</option>
</select>
#endforeach
</div>
<div class="col-lg-8 my-auto ">
#foreach ($main_ingredientsRecipeName as $index => $main_ingredient)
<input style="border: none; outline:none;" type="text" name="main_ingredient[{{
$index }}]"
class="my-1 py-1 px-2 rounded" placeholder="Place Ingredients here"
wire:model="main_ingredientsRecipeName.{{ $index }}.main_ingredient" required
/>
<a href="#" wire:click.prevent="removeMainIngredient({{ $index }})">
<button style="background-color: rgba(196, 72, 72, 0.644);" class=" mx-1 btn
btn-sm text-light">
Delete
</button>
</a>
#endforeach
</div>
</div>
<div class="text-start py-2">
<div>
<button style="opacity: 0.6" class=" btn btn-sm btn-light text-black"
wire:click.prevent="addMainIngredient">
Add Ingredient
</button>
</div>
<br>
{{-- CAN GET SUM AND DIVIDE BUT IT MUST RETURN WITH MEASUREMENT --}}
#foreach ($main_ingredientsNumberPortion as $index => $num)
#if (($measurement[$index] ?? 0) == 'mL')
{{-- IF MEASUREMENT IS ML --}}
{{ $total += (int) $num['numberPortion'] * 1 }}
#elseif (($measurement[$index] ?? 0) == 'cups')
{{-- IF MEASUREMENT IS CUPS --}}
{{ $total += (int) $num['numberPortion'] * 183 }}
#elseif (($measurement[$index] ?? 0) == 'liter')
{{-- IF MEASUREMENT IS LITER --}}
{{ $total += (int) $num['numberPortion'] * 1000 }}
#elseif (($measurement[$index] ?? 0) == 'kgs')
{{-- IF MEASUREMENT IS KGS --}}
{{ $total += (int) $num['numberPortion'] * 1000 }}
#elseif (($measurement[$index] ?? 0) == 'grams')
{{-- IF MEASUREMENT IS KGS --}}
{{ $total += (int) $num['numberPortion'] * 1 }}
#else
{{-- DEFAULT MEASUREMENT PCS --}}
{{ $total += (int) $num['numberPortion'] * 100 }}
#endif
#endforeach
</div>
<div class="row pt-3">
{{-- <h5 class="">Serving:</h5> --}}
<div class="">
<label for="serving" class="text-light">Serving - Auto-Calculate:</label>
<br>
<input style="border: 1px green solid; outline:none;" class="typeahead rounded py-1
px-3 w-50"
type="number" name="serving" id="serving" value="{{ $total / 300 }}" />
</div>
</div>
</div>

Submit form from a tab content

I'm new at Laravel. How can I submit form from a tab content and return to the same tab content view? Example, I choose November form then when I submit it return to November. I'm really confusing right now.
This is my controller :
public function project_charter()
{
$nik = Sentinel::getUser()->nik;
$data = AccelerateMentee::select(
'accelerate_mentee.*',
'accelerate_rotation.start_date',
'accelerate_rotation.id as id_rotation',
'accelerate_rotation.status as status_rotation'
)
->leftJoin('accelerate_rotation', 'accelerate_rotation.id', '=', 'accelerate_mentee.rotation')
->where([
['accelerate_mentee.nik', '=', $nik],
])
->first();
if(!empty($data)){
$first_month = date('m', strtotime($data->start_date));
} else {
$first_month = null;
}
$project_activity = AccelerateProjectActivity::where([
['id_rotation', '=', $data->id_rotation],
['month', '=', $first_month]
])->get();
This is my view :
<div class="tab-content mt-3 ">
<ul class="nav nav-pills nav-white" id="pills-tab" role="tablist">
#for($i = 0; $i < 6; $i++)
#php $month = date('m', mktime(0, 0, 0, ($first_month + $i), 1)); #endphp
<li class="nav-item">
<a class="nav-link {{$i == 0 ? 'active' : ''}} get-project-activity" data-month="{{ $month }}" href="{{'#table_'. $month }}" data-toggle="tab">
{{ date('F', mktime(0, 0, 0, ($first_month + $i), 1)) }}
</a>
</li>
#endfor
</ul>
<div class="tab-content" id="pills-tabContent">
#for($i = 0; $i < 6; $i++)
#php $month = date('m', mktime(0, 0, 0, ($first_month + $i), 1)); #endphp
#if($i == 0)
<div class="tab-pane show mt-5 {{$i == 0 ? 'active' : ''}}" id="{{'table_' . $month }}" role="tabpanel">
<div class="card my-2">
<div class="card-body">
<div class="d-flex justify-content-between pb-5">
<h3>Project Activity {{ date('F', mktime(0, 0, 0, ($first_month + $i), 1)) }}</h3>
#if($data->status_rotation == 'draft')
<button class="btn btn-primary btn-sm create-activity" data-month="{{ $month }}">
<i class="fa fa-plus"></i>
Add Project Activity
</button>
#endif
</div>
<div class="mb-5">
#include('pages.accelerate.mentee.element.table_project_activity')
</div>
</div>
</div>
</div>
#else
<div class="tab-pane mt-5" id="{{'table_' . $month}}" role="tabpanel" aria-labelledby="data">
<div class="card my-2">
<div class="card-body">
<div class="d-flex justify-content-between pb-5">
<h3>Project Activity {{ date('F', mktime(0, 0, 0, ($first_month + $i), 1)) }}</h3>
#if($data->status_rotation == 'draft')
<button class="btn btn-primary btn-sm create-activity" data-month="{{ $month }}">
<i class="fa fa-plus"></i>
Add Project Activity
</button>
#endif
</div>
<div class="mb-5" id="{{'detail_' . $month}}">
</div>
</div>
</div>
</div>
#endif
#endfor
</div>
</div>
Hope u guys can help me, I try many ways but didn't work. I'm really really confusing. Thanks!

How do I get total in a foreach loop

I have a div that displays items , quantities and total prices for each item using a foreach loop. I now want to get the total amount and dont seem to get it.
This is my blade file
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
<?php
$total = 0;
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
?>
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
<?php $total_amount += $total?>
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
I have also tried <div><strong class="order-total">KSh <?php echo $total_amount?> </strong></div> but still does not work.
I get an error of undefined variable total_amount. How do I go about it?
The variables $total and $total_amount must be outside of the foreach
here I have rewritten your code
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
#php $total = 0; $total_amount = 0; #endphp
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
#php
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
#endphp
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
#php $total_amount += $total; #endphp
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>

Translate to Blade+Laravel a while loop

This code works, however in my learning of Laravel, I want to know if using Blade+Laravel syntax, can be better implemented
<?php
$i = 1;
while ($i <= (5 - $post->images->count())) {
echo '<div class="col"> </div>';
$i++;
}
?>
Thanks
https://laravel.com/docs/5.5/blade#loops
I would suggest using a for loop instead of a while loop in this case:
#for ($i = 1; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
#endfor
I'm not sure is the best way to do, but works.
<?php $z = 0; ?>
#while ($z < 3)
{{ "test".$z }}
<?php $z++ ?>
#endwhile
#php
$i = 0;
#endphp
#while (++$i <= (5 - $post->images->count()))
<div class="col">
</div>
#endwhile
#for ($i = 0; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
#endfor
Yes, there is. Templating is made just for that, you can see how similar things are done with the docs : laravel blade : loops
#for ($i = 0; $i < $post->images->count()); $i++)
<div class="col"> </div>
#endfor
The better solution would be to use #foreach
#foreach( $image as $post->images )
<div class="col"> </div>
#endforeach
In this specific case beacause you are looping count() you should use foreach, however you may also be interested #forelse:
#forelse($image as $img)
<div class="col"> {{ $img }}</div>
#empty
<div class="col"> NO IMAGES </div>
#endforesle

Method appends does not exist Laravel

I want to select all hotels where minrate between 0 and 49 it marche perfectly , but when i want to select all hotels where minrate between 0-49 and 50-99 It is you to say when it enters the loop "for" it return this error
ErrorException in Macroable.php line 74: Method appends does not exist. (View: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php) (View: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php)
Function Laravel
public function pricefilter(Request $request)
{
$query = Input::get('query', false);
$brans = request()->priceFilter;
$books = new Collection();
if (!empty($brans)) {
$product = "";
foreach ($brans as $data) {
$minMax = explode("-",$data);
$product[] = Afica::search()
->multiMatch(['name','city_hotel'], $query, ['fuzziness' => 'AUTO'])
->filter()
->range('minrate',['from'=>$minMax[0],'to'=>$minMax[1]])
->paginate(26)
->appends('priceFilter' , request('priceFilter'));
}
$books = $product[0];
for ($i = 1; $i < count($product); $i++) {
$books = $books->union($product[$i]);
}
} else {
$books = Afica::search()
->paginate(26);
}
if ($request->ajax()) {
return view('presult', compact('books'));
}
return view('welcome',compact('books'));
}
View presult
<!--deal-->
<?php $myArray = array();?>
#if (empty($myArray))
#foreach($books as $Afica)
<?php $collection = collect($Afica);?>
<article class="one-third">
<figure><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></figure>
<div class="details">
<h3>{{{ $collection->get('name') }}}
<span class="stars">
<?php
for ($i=1 ; $i<= $collection->get('class') ; $i++)
{
echo ' <i class="material-icons"></i>';
}
?>
</span>
</h3>
<span class="address">{{{ $collection->get('city_hotel') }}},{{{ $collection->get('address') }}} Show on map</span>
<span class="rating">{{{ $collection->get('preferred') }}}</span>
<span class="price">Max rate <em>$ {{{ $collection->get('maxrate') }}}</em> </span>
<span class="pricee">Min rate <em>$ {{{ $collection->get('minrate') }}}</em> </span>
<div class="description">
<p>{{{ $collection->get('desc_en') }}}More info</p>
</div>
Book now
</div>
</article><?php $myArray[] = $collection->get('id');?>
<!--//deal-->
#endforeach
#endif
<!--//bottom navigation-->
<div class="bottom-nav">
Back up
<div class="pager">
<?php $query=Input::get('query', '');?>
#if($query == "")
<span>First Page</span>
#else
<span><a href="?query=<?php echo $query?>&page=1"=>FirstPage</a></span>
#endif
{!! $books->appends(['query' => Input::get('query')])->render() !!}
#if($query == "")
<span>Last Page</span>
#else
<span>Last Page</span>
#endif
<!--bottom navigation-->
</div>
</div>
<!--//bottom navigation-->
It looks like you are calling ->appends on your variable called $books in several places within your view.
If $books is empty (i.e. null) this might be causing your problems.
I would suggest wrapping your $books in if is set functions as a starting point
if (isset($books) && $books != NULL) {
$books->appends(['query' => Input::get('query')])->render()
} else {
// Do something which tells the user that there are no books
}
An even better solution would be to handle all of the logic for the view in your controller and use something like a #forelse loop instead of a #foreach loop. This would make it easier to handle situations where you return an empty collection.
#forelse ($books as $book)
// Do your for each book markup
#empty
// Do markup which shows when you have no books
#endforelse
Corrections to the template
<?php $myArray = array(); ?>
#if (empty($myArray))
#foreach($books as $Afica)
<?php $collection = collect($Afica);?>
<article class="one-third">
<figure><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></figure>
<div class="details">
<h3>{{{ $collection->get('name') }}}
<span class="stars">
<?php
for ($i=1 ; $i<= $collection->get('class') ; $i++) {
echo ' <i class="material-icons"></i>';
}
?>
</span>
</h3>
</div>
<span class="address">{{{ $collection->get('city_hotel') }}},{{{ $collection->get('address') }}} Show on map</span>
<span class="rating">{{{ $collection->get('preferred') }}}</span>
<span class="price">Max rate <em>$ {{{ $collection->get('maxrate') }}}</em> </span>
<span class="pricee">Min rate <em>$ {{{ $collection->get('minrate') }}}</em> </span>
<div class="description">
<p>{{{ $collection->get('desc_en') }}}More info</p>
</div>
<div>
Book now
</div>
</article><?php $myArray[] = $collection->get('id');?>
#endforeach
#endif
<div class="bottom-nav">
Back up
<div class="pager">
<?php $query=Input::get('query', '');?>
#if($query == "")
<span>First Page</span>
#else
<span>FirstPage</span>
#endif
{!! $books->appends(['query' => Input::get('query')])->render() !!}
#if($query == "")
<span>Last Page</span>
#else
<span>Last Page</span>
#endif
</div>
</div>

Resources