I have a problem with array group. Could you please help me to solve this problem. I want to group acording to the $group array value. For a Example, first Group 01 items & second Group 02 items in the table.
// In Controller
public $inputs = [0, 1, 2];
public $item_id = [18, 19, 20], $group = ['Group 01', 'Group 02', 'Group 01'];
//In blead
<tbody class="text-xs divide-y divide-gray-100 ">
#forelse ($inputs as $key => $value)
<tr>
<td class="p-2">
{{ $group[$value] }}
</td>
<td class="p-2">
{{ $item_id[$value] }}
</td>
</tr>
#empty
#endforelse
If Can I want give group name each group strat before first item like topic. Thank you
Edit 01
I am making a Laravel livewire application. In my application, I am adding dynamic inputs and I want to show it my blead view.
As the image, When clicking the No 1, will come popup, Then enter the records and add. it will display in the table such as No 3 & 4. But I want to group them according to the group. No 3 items are the same group No 4 is another group. But in here showing according to the inputs array.
In my controller I did,
public $i = -1;
public $inputs = [];
public $category_ids, $item_id, $group;
public function addLine($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs, $i);
}
You can create a group items array by the following code. It will make a nested array of items for each group then you can use them by looping
$group = ['Group 01', 'Group 02', 'Group 01'];
$item_id = [18, 19, 20];
$group_items= [];
foreach($group as $key => $value) {
$group_items[$value][] = $item_id[$key];
}
In your blade file
#foreach ($group_items as $group => $items)
<tr>
<td class="p-2">
{{ $group }}
</td>
<td class="p-2">
#foreach ($items as $item)
{{ $item }}
#endforeach
</td>
</tr>
#endforeach
Related
I want to retrieve some data from the DB with for loop, So at the Controller, I have added this:
$foundOrder = Cart::select('crt_content')->whereCrtId($cart->crt_id)->latest()->first();
$foundOrder = json_decode($foundOrder->crt_content);
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
$prds[] = [
Product::with('uploaded')->wherePrdId($value->id)->select('*')->first(),
$value->quantity,
$value->price
];
}
} else {
$prds = [];
}
So $prds is an array and if I do {{ dd($prds) }}, I get this:
So at the Blade, I tried this:
#for($i=0;$i<=count($prds);$i++)
{{ $prds[$i]->prd_name }}
{{ $prds[$i]->prd_sale_price }}
#endfor
But it returns this error:
Trying to get property 'prd_name' of non-object
So what is wrong here ? As you can see in the picture, I'm getting all the existing products with their attributes (such as prd_id, prd_name, prd_sale_price).
You are storing Product object, quantity and price in your $prds array. And you have array inside array. Use below code
Using foreach loop
#foreach($prds as $data)
// in data 0 index will your product object,1 will be quantity and 2 will be price
{{ $data[0]->prd_name }} // prd_name from product object
{{ $data[0]->prd_sale_price }}
#endforeach
// using for loop
#for($i=0;$i<count($prds);$i++)
{{ $prds[$i][0]->prd_name }}
{{ $prds[$i][0]->prd_sale_price }}
#endfor
Your $prds is an array with each element have below structure:
[
product,
quantity,
price
];
So if you loop through it and want to get attributes of a product, you need to get it from $prds[$i][0], for example: $prds[$i][0]->prd_name.
Update: You need to loop from index 0 to count($prds) - 1:
#for($i = 0; $i <= count($prds) - 1; $i++)
i've tried so many step to make this table(the second table):
second table
with this array
{
"Habie": {
"2019-07-17": [
{
"durasi": "0 Jam",
"status_durasi": "Kurang"
}
],
"2019-07-07": [
{
"durasi": "0 Jam",
"status_durasi": "Kurang"
}
]
},
"Budiman": {
"2019-07-13": [
{
"durasi": "0 Jam",
"status_durasi": "Kurang"
}
]
}
}
and with this array date interval for the table header:
[
"2019-07-20",
"2019-07-19",
"2019-07-18",
"2019-07-17",
"2019-07-16",
"2019-07-15",
"2019-07-14",
"2019-07-13",
"2019-07-12",
"2019-07-11",
"2019-07-10",
"2019-07-09",
"2019-07-08",
"2019-07-07"
]
then, in blade i make looping like this:
<tr>
<th>NAMA</th>
#foreach ($tanggalInterval as $key => $item)
<th>{{$item}}</th>
#endforeach
</tr>
<tr>
#foreach ($kehadiran as $kData=> $data)
<td>{{$kData}}</td>
#foreach ($data as $kTanggal => $tanggal)
#foreach ($tanggal as $kDrsSts => $item)
#foreach ($item as $kItem => $drs)
{{-- {{dd()}} --}}
{{-- <td>{{$item["durasi"]}}</td> --}}
#endforeach
#endforeach
#endforeach
#endforeach
</tr>
Nb: grand total is found from the total length of work(durasi)
but, no one step work :(.
i hope someone can help me :)
thanks :D
EDITED:
from this database table:
database Table
to be:
The table that i want
In my blade.php file, I'm trying to merge two foreach loop to display data in the table. I had tried use something like nested loop but it doesn't work.
#foreach($cat->products as $idx => $product)
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
</tr>
#endforeach
#endforeach
I just need to insert the total data into the table. Now the total is displayed correctly but it will duplicate the sku and name in the table.
Define an array which we will use as a "flag" and on each iteration check if the current product SKU is NOT in our "flag" array. If it isn't then we display and we add the current product SKU to the list of processed SKUs and continue as normal.
#php $skus = [] #endphp
#foreach($cat->products as $idx => $product)
#if (!in_array($product->sku, $skus))
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
#endforeach
</tr>
#php array_push($skus, $product->sku); #endphp
#endif
#endforeach
Note: You are using Laravel 4, the current version of Laravel is 5.7, I would absolutely update and use the latest version.
Reading Material
in_array
array_push
table(skill)
have many skills, columns type boolean(0,1),by laravel
i want to get the names of columns if skills value equal 1 and user_id equal 1,
without say the name of column becouse they 50 skills ,i tried this on sublime
<?php
$variable2=App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->get();
?>
#foreach($variable2 as $key1 => $value1)
#if($value1='1')
<span class="tags">{{ $key1 }}</span>
#endif
#endforeach
Well, looks like you forgot, that get returns a collection of skills. You must use two loops.
<?php
$userSkills = App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->get();
?>
<!-- loop through a list of user skills (get gives us a set of skills -->
#foreach($userSkills as $skill)
<!-- loop through skill properties -->
#foreach($skill->toArray() as $key => $value)
#if($value)
<span class="tags">{{ $key }}</span>
#endif
#endforeach
#endforeach
<?php $variable2=App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->where('user_id', 1)
->get();
$skills = $variable2->filter(function ($item) { return $item === 1; })->keys();
?>
#foreach($skills as $key)
<span class="tags">{{ $key }}</span>
#endforeach
I am using the paginator of Laravel to display 100 results per page. In the front of each row I'm currently displaying the ID of the database field. However, I want to display the count.
I've found some threads on stackoverflow solving this issue for Laravel 4
Where they say, you should solve it like this
<?php $count = $players->getFrom(); ?>
#foreach ($players as $player)
<tr>
<td>{{ $count++ }}. </td>
</tr>
#endforeach
or like this
<?php $count = $players->getFrom() + 1; ?>
#foreach ($players as $player)
...
The problem here, is that Laravel 5.1 doesn't have the getForm method anymore. The upgrade guide says to replace getFrom by firstItem, which I did. Unfortunetely, I'm not getting the correct numbers.
I tried it like this
<?php $count = $pages->firstItem() + 1 ?>
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
and like this
{{ $count = $pages->firstItem() }}
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
//pages.singlebasic.blade.php
{{ $count ++ }}
but my site always displays only the number "2" instead of counting up. How do I achieve that?
Just use this to get pages count
$players->lastPage()
or this to get items count
$players->total()
If you want every entry count DONT DO {{ $count++ }} because its echoing data. Instead do like that
<?php $count++; ?>
{{ $count }}
First of all get 100 players with paginate(100) method then you can get the index or count in the following way:
<?php $count = 1; ?>
#foreach ($players as $player)
<tr>
<td>{{$players ->perPage()*($players->currentPage()-1)+$count}}</td>
</tr>
<?php $count++; ?>
#endforeach
Here {{$players ->perPage()*($players->currentPage()-1)+$count}} will print the index or count like 1 to 100 on first page and 101 to 200 on next page and so on.
If you want to use laravel 5 loop itration
`#foreach ($players as $player)
<tr>
<td>{{$players->perPage()*($players->currentPage()-1)+$loop->iteration}}</td>
</tr>
#endforeach`
Hope this helps.
What i used to do in order to avoid php tags in blade templates in 4.2 was something like that
#foreach ($players as $key => $player)
<tr>
<td>{{ (Input::get('page', 1) - 1) * $players->getPerPage() + $key + 1 }}. </td>
</tr>
#endforeach
It's kinda strange but it will do the trick.