Fetching the array data and using explode i have separated the content. Now, i want to print those data in the list. But products are printing in the line. I tried using "break tag" And "list tag" Every possible ways.
Here is the code
<tbody id="test">
#foreach ($data['samplingData'] as $key => $samplingData)
<tr> <td>{{$samplingData->doctor_name}}</td>
<td>{{$samplingData->date}}</td>
#foreach(explode(',', $samplingData->products) as $product)
<td> {{$product}} </td>
#endforeach
<td>{{$samplingData->quantity}}</td> </tr>
#endforeach
</tbody>
Data is printing in this way
Name date products quantity
a 12-12-12 a a a a 1 1 1 1
I want to print like this
Name date products quantity
a 12-12-12 a 1
a 1
a 1
I tried adding tr inside td and as well as list inside the td.
Please help me in formatting the data.
Basically you were creating <td> for-each $product.
<td>#foreach(explode(',', $samplingData->products) as $product) {{$product}} <BR/> #endforeach</td>
Related
I am making a Crud application. i have a database table called 'tag' in here is a column called 'color'
In my index.blade.php (for the crud) i want my table row to be the color i put in my database, by string.
Data example;
[id = '1', name = 'dutch', color = 'red']
code i currently have in my blade file:
#foreach($tags as $tag)
<tr>
<td>{{$tag->id}}</td>
<td>{{$tag->name}} </td>
<td style="background:{{$tag['color']}} ;">{{$tag->color}} </td>
You should Add background-color, It would be Much better if you can save Color codes instead of strings in DB.
<td style="background-color:{{$tag['color']}} ;">{{$tag->color}</td>
i have a layout that shows all the orders placed in which i have record of paid and remaining amount also , so i am trying to display the background color of rows as red if remaining field data is more than 0 for this i am trying this method
public function noBalance() {
return Order::where('remaining', '>', 0)->first();
}
i am creating this in model file of Order
also tried this
return Order::where('remaining', '>', 0);
and
#foreach ($orders as $order)
<tr style="{{ $order->noBalance() ? 'background-color: lightcoral;' : '' }}">
(here i am using that function in my allorder.blade.php)
<td>{{$order->id}}</td>
<td>{{$order->client}}</td>
<td>{{$order->salesmanRelation->employee_name}}</td>
<td>{{$order->orderBooker->employee_name}}</td>
<td>{{$order->total_amount}}</td>
<td>{{$order->paid}}</td>
<td>{{$order->remaining}}</td>
<td>{{$order->discount}}</td>
<td>{{$order->created_at->toFormattedDateString()}}</td>
<td>Detail</td>
<td>Edit</td>
</tr>
#endforeach
but after using this all the rows of my table gets light coral not only the rows with the remaining >0
please help!
If there is a field 'remaining' in your database you can access it with:
$order->remaining;
So your if statement should look like this:
{{ $order->remaining > 0 ? 'background-color: lightcoral;' : '' }}
And the function noBalance() can be removed.
yes that is the right answer
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
I send two variable to my view $abilities, and $users. $abilities is an array of key-value pairs containing data in the following format:
["1"=>"create_user","2"=>"delete_user","3"=>"ban_user"]
$users is an array in the following format:
["1"=>"John Doe","2"=>"Jane Doe"]
Both are taken from their respective databases abilities (columns: ID, name) and users (columns: ID, name).
In the view, I have a table which contains a multiple select for each row against the $user->id:
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>
{{Form::select("takenabilities[]",$abilities, null, ['id'=>'system-abilities','multiple','class'=>'form-control' ])}}
</td>
</tr>
#endforeach
Problem is that when I receive the request in my controller, I only see one takenabilities[] array, and I want to save data from each multiple select in each row generated dynamically, if that makes sense?
You can't do that using multiple selects with the same name. The value of the last one overrides all previous selects' values. In addition you'd better assign different id to every select. Try to add another level to the array:
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>
{{Form::select("takenabilities[$user->id][]", $abilities, null, ['id'=>'system-abilities-' . $user->id,'multiple','class'=>'form-control' ])}}
</td>
</tr>
#endforeach
And in controller:
$takenabilities = [];
foreach(Request::input('takenabilities') as $userId => $userAbilities) {
$takenabilities = array_merge($takenabilities, $userAbilities);
}
$takenabilities = array_unique($takenabilities);
I have a table and I want to enumerate each element.
Something like this
#foreach($elements as $element)
<tr>
<td>{{ (isset($a))?$a++:($a = 1) }}</td>
<td>...
</tr>
#endforeach
I expect the table to begin with 1 and then count on, however, the first two columns are always 1. I have already solved this problem by giving the template an $a = 0; on the controller but I want to know why my first solution doesn't work and if there is a workaround
Laravel Blade has a very handy variable for loops, called the $loop variable. This lets you gather information about the current element of the list, including the index and iteration count.
So you can do just this:
#foreach($elements as $element)
<tr>
<td>{{ $loop->iteration }}</td>
<td>...
</tr>
#endforeach