Laravel loop issue - php

I need help with looping my product options, this is what i have now:
What I want is simply get 2 row's only one for color and another for size and in dropdown in front of each one have all items.
here is my blade code:
<tbody>
#foreach($product->suboptions as $option)
<tr>
<td style="width: 150px;">{{ $option->option->title }}</td>
<td class="text-left">
<select name="" id="">
<option value="{{$option->id}}">{{$option->title}} - {{ number_format($option->price, 0) }}</option>
</select>
</td>
</tr>
#endforeach
</tbody>
UPDATE
my dd result of {dd($product->suboptions)}}

You can first group them together with the mapToGroups() function for a collection
https://laravel.com/docs/5.5/collections#method-maptogroups
$something = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});
You should dd() that to see what the output is and understand it.
After that you can just cycle through them with foreach, your blade should be something like
<tbody>
#foreach($something as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="" id="">
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>

For accomplishing that you need to get option first, and write a method to access options via products id or products object. so you can right something like this in products model:
public function get_option($product_id){
$sub_options = Product::whereId($product_id)->first()->suboptions()->get();
$option = array();
foreach($sub_options as $sub_option){
$option[] = $sub_option->options()->get();
}
return $option;
}
And then in view, you just call this method and put it in 2 foreach, one for option and the other for sub-option. like this code below:
<tbody>
#foreach($product->get_option($product->id) as $key=>$value)
<tr>
<td style="width: 150px;">{{ $value->title }}</td>
<td class="text-left">
<select name="" id="">
#foreach($value->suboptions() as $key=>$value2)
<option value="{{$value2->id}}">{{$value2->title}} - {{number_format($value2->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
I don't know whats your model methods but you can get the concept.
I Repeat again
you should not copy my code exactly. just follow along with the concept of what am I saying.
But this time according to your eloquent methods. this code should work.

Related

Passing variable from index to controller using Laravel

I am new to using Laravel and I am currently trying to pass a variable from the index blade file to the controller in order to bring up all apparatus type fields associated with a specific apparatus code id.
I am able to access the apparatus type fields associated with the apparatus code id when I include a specific number which correlates to that apparatus code id (eg. inputting 2 brings up the apparatus type details for the apparatus code id 2, inputting 3 brings up the apparatus type details for the apparatus code id 3 etc).
However when I have tried to pass a variable relating to each individual apparatus code id using a for loop in the controller, the apparatus type loop appears to not be accessed and does not return any information. I will include snippets of my current code for clarity.
Any help or guidance with this issue would be greatly appreciated!
Controller file
public function index()
{
$apparatusCodes = ApparatusCodes::get();
foreach ($apparatusCodes as $apparatusCode){
$apparatusTypes = ApparatusTypes::where('apparatus_code_id', $apparatusCode->id)->get();
}
return view('apparatus_codes.index', compact('apparatusCodes', 'apparatusTypes'));
}
Index.blade file
<table id="datatable" class="stripe hover dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th>ID</th>
<th >Apparatus Code</th>
<th>Description</th>
<th>Rent</th>
<th></th>
</tr>
</thead>
<!--start of for loop-->
#foreach ($apparatusCodes as $apparatusCode)
<tbody>
<tr data-toggle="collapse" id="table{{ $apparatusCode->id}}" data-target=".table{{ $apparatusCode->id}}">
<td> {{ $apparatusCode->id}} </td>
<td>{{ $apparatusCode->apparatus_code}} </td>
<td> {{ $apparatusCode->description}}</td>
<td> {{ $apparatusCode->rent}}</td>
<td #click="isOpen = !isOpen" class="main-bg"><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/Icon-arrow-dropdown-white.png" alt="arrow down">
</td>
<td><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/edit-icon.svg" alt="Edit Icon">
</td>
</tr>
<tr x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak #click.away="isOpen = false" class="collapse table{{ $apparatusCode->id}}">
<td colspan="999">
<div>
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>Apparatus Code </th>
<th>Apparatus Type</th>
<th>Compensation </th>
<th>Created On </th>
<th>Created By </th>
<th></th>
#foreach ($apparatusTypes as $apparatusType)
</thead>
<tbody>
<tr>
<td>{{ $apparatusCode->apparatus_code}}</td>
<td>{{ $apparatusType->apparatus_type }}</td>
<td>{{ $apparatusType->compensation }}</td>
<td>{{ $apparatusType->created_at }}</td>
<td>{{ $apparatusType->users->name}}</td>
<td> <button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/Icon-edit-gray.png" alt="edit"></button></td>
</tr>
#endforeach
</table>
</tr>
</tr>
</td>
</tbody>
#endforeach
</table>
If you have the relationships setup you could eager load the ApparatusTypes for each ApparatusCode:
$apparatusCodes = ApparatusCodes::with('appartusTypes')->get();
Then in the view you could iterate the appartusTypes of each ApparatusCode:
#foreach ($apparatusCodes as $apparatusCode)
...
#foreach ($apparatusCode->apparatusTypes as $type)
...
#endforeach
...
#endforeach
Without using the relationships you could get all the ApparatusTypes for the ApparatusCodes and then group them by apparatus_code_id:
$codes = ApparatusCodes::get();
$types = ApparatusTypes::whereIn('apparatus_code_id', $codes->pluck('id'))
->get()
->groupBy('apparatus_code_id');
Then in the view you could iterate the group that corresponds to the current Code's apparatus_code_id:
#foreach ($codes as $code)
...
#foreach ($types->get($code->id, []) as $type)
...
#endforeach
...
#endforeach
Laravel 8.x Docs - Eloquent - Relationships - Eeager Loading with
Laravel 8.x Docs - Collections - Available Methods - pluck
Laravel 8.x Docs - Collections - Available Methods - groupBy

Is it possible to use mutators on array? Laravel

I have a table like this:
#foreach($order->cart->items as $item)
<tr>
<th scope="row"><img class="basketimg mr-1" src="/img/products/{{$item['img']}}"><span class="basket-prod-name">{{ $item->Short_Prod_Name() }}</span></th>
<td class="text-center">
<div class="prodcount">{{$item['qty']}}</div>
</td>
<td class="text-right">{{$item['cost']}}AZN</td>
</tr>
#endforeach
And for the name, I want to use a murator to strip out the name that is too long.
Here is:
public function getShortProdNameAttribute()
{
return substr($this->name, 0, 10);
}
Now how can I use it?
$item['Short_Prod_Name'] doesn't work, this is the first time I try to use a mutator on an array. How can i do this?
P.S.In other questions, they wrote that it is better to use casts, but still, is the mutator applicable here?
According to the Laravel convention, the getShortProdNameAttribute() mutator will resolve to the short_prod_name attribute:
$item->short_prod_name
Rememeber to append the additional attribute in the Item class:
protected $appends = ['short_prod_name']
Thusly, the full code snippet is as follows:
#foreach($order->cart->items as $item)
<tr>
<th scope="row"><img class="basketimg mr-1" src="/img/products/{{$item['img']}}"><span class="basket-prod-name">{{ $item->short_prod_name }}</span></th>
<td class="text-center">
<div class="prodcount">{{$item['qty']}}</div>
</td>
<td class="text-right">{{$item['cost']}}AZN</td>
</tr>
#endforeach
An alternative solution to address our problem is to use Str::limit:
#foreach($order->cart->items as $item)
<tr>
<th scope="row"><img class="basketimg mr-1" src="/img/products/{{$item['img']}}"><span class="basket-prod-name">{{ Str::limit($item->name, 10) }}</span></th>
<td class="text-center">
<div class="prodcount">{{$item['qty']}}</div>
</td>
<td class="text-right">{{$item['cost']}}AZN</td>
</tr>
#endforeach

How to list dynamic tables data in foreach loop

i have dynamic table and that table datas are listed one table i will try this way
$cat= DB::table('categories')->get(); //these is take table name
foreach($cat as $data){
$res= DB::table($data->name)->get(); //query build
}
return view('admin.company')->with('result',$res)->with('catg',$cat); // pass to view page
and i print data in view page but it list one table data only i print like this
#foreach($result as $datas)
<td > {{$datas->phone}} </td>
<td > #foreach($catg as $cat) #if($cat->id==$datas->cat_id){{$cat->name}} #endif #endforeach </td>
<td > {{$datas->discription}} </td>
#endforeach
how to list all tables data in one view any way? i am using laravel new version !
This is failing to work because you are assigning to $res each time in your loop, so it will only be the last table information that shows.
Change the inner part of your loop to $res[] = DB::table($data->name)->get();
You will then need to create an outer loop in your view, as such
#foreach($res as $table)
#foreach($table as $datas)
<td > {{$datas->phone}} </td>
<td > #foreach($catg as $cat) #if($cat->id==$datas->cat_id){{$cat->name}} #endif
#endforeach </td>
<td > {{$datas->discription}} </td>
#endforeach
#foreach
It may help also in your question to add why you are trying to what you are doing. I can see many other problems with your approach, but without knowing your data, it is hard to provide a better solution.
You can do that more cleverly
$cat= DB::table('categories')->get(); //these is take table name
foreach($cat as $data){
$res[$data->name] = DB::table($data->name)->get(); //query build
// or
// $res[] = [
// 'category' = $data,
// 'data' => DB::table($data->name)->get()
//]
}
return view('admin.company')->with('result',$res);
In view
#foreach($res as $tableName => $data)
#foreach($data as $datas)
<td > {{$datas->phone}} </td>
<td > {{$tableName}} </td>
<td > {{$datas->discription}} </td>
#endforeach
#foreach

Laravel - How to use rowspan inside foreach loop?

I have a problem to print rowspan inside a foreach loop. I want the result to look like this,
Result that I expected (code with HTML)
I write my code in php file called,
project-detail.blade.php
<?php $no = 1; ?>
#foreach($tasks as $task)
<tr>
<td rowspan="{{ count($tasks) }}">{{ $no }}</td>
<td rowspan="{{ count($tasks) }}">{{ $task->name }}</td>
<td onclick="getTaskDescription('{{$task->task_id}}')">
{{ $task->task_detail }}(Click for description)
</td>
<td>83%</td>
<td>Not yet</td>
<td rowspan="{{ count($tasks) }}">
<button class="btn btn-info btn-sm" style="border-radius: 0"><i class="icon icon-plus" style="font-size: 12px">
</i> Add Task</button>
</td>
</tr>
<tr>
<td>Second task (Click for description)</td>
<td>50%</td>
<td><button class="btn btn-default btn-sm">Review</button></td>
</tr>
<?php $no++; ?>
#endforeach
However, I am not getting expected result. Result of my code looks like this,
Result link
Can anyone help me? Thanks.
I think your code show correct view. Give some bottom padding on td . Hope you will be get your excepted view.

Styling table in Laravel

I have a problem with #foreach in my blade.php. When I insert movie with multiple categories my other moves to the right. Here is how it looks in browser
and here is code in blade
#if($movies)
<div class="row">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Actors</th>
</tr>
</thead>
<tbody>
#foreach($movies as $movie)
<tr>
<td>{{$movie->name}}</td>
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
#foreach($movie->actors as $actor)
<td>{{$actor->actor_name}}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
#endif
Your issue lies here:
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
You are saying that for each category, it should insert a new <td> into you table. This will of course skew the content since your table expects a set number of columns.
Instead, you should move your foreach statement inside the <td></td>
<td>
#foreach($movie->categories as $category)
{{$category->category_name}}
#endforeach
</td>
This will print the links for each category in the same column.

Categories