Laravel - Illegal string offset - php

I try to color table cell in laravel based on cell content but i keep getting this error:
"Illegal string offset 'Disponible' (View:
C:\Users\RAYLAN\Documents\CRMSAV\resources\views\
pagination_data.blade.php)
(View: C:\Users\RAYLAN\Documents\CRMSAV\re..."
This is my code:
#foreach($data as $row)
<tr>
<td>{{ $row->ID_Piece }}</td>
<td>{{ $row->Designation }}</td>
<td style="background-color: {{ $row->Status['Disponible'] }}">
{{ $row->Status }}
</td>
</tr>
{{$row->Status = array('Disponible' => '#FF0', 'N' => '#F0F')}}
#endforeach
<tr>
<td colspan="3" align="center">
{!! $data->links() !!}
</td>
</tr>

It's saying that there is no Disponible in this line:
{{$row->Status = array('Disponible' => '#FF0', 'N' => '#F0F')}}
Perhaps write this on the line above and check what is in $row->Status:
<?php
dd( $row->Status );
?>
But it looks a bit wierd, to be honest. Remember that the double-mustaches ( {{ $foobar }} ) are echoing the content. But you're assigning a value in there... To something that you're looping through?!

if your $row->Status['Disponsible'] is present in all the rows then try the below
<td style="background-color: {{ $row->Status['Disponible'] }}">
{{ $row->Status['Disponsible'] }}
</td>
EDIT:
try to substitute for your #foreachloop
#foreach($data as $row)
{{$row->Status = array('Disponible' => '#FF0', 'N' => '#F0F')}}
<tr>
<td>{{ $row->ID_Piece }}</td>
<td>{{ $row->Designation }}</td>
<td style="background-color: {{ $row->Status['Disponible'] }}">
{{ $row->Status['Disponible'] }}
</td>
</tr>
#endforeach

I decided to do it with Jquery, here is the working solution :
#foreach($data as $row)
<tr>
<td>{{ $row->ID_Piece }}</td>
<td>{{ $row->Designation }}</td>
<td id="status">{{ $row->Status }}</td>
</tr>
#endforeach
<tr>
<td colspan="3" align="center">
{!! $data->links() !!}
</td>
</tr>
<script type="text/javascript">
$(document).ready(function(){
$('#status').each(function(){
if ($(this).text() == 'N') {
$(this).css('background-color','#f00');
}
});
});
</script>

Related

How can i add two numbers in foreach blade in laravel and display the total

I need to add two numbers passed by the foreach in the blade view
how can i add this two numbers instead of showing it as individual 333.34 333.34,
I need to show it as 666.68 / 1000 AUD
Here's how blade foreach looks like
#if(!empty($teams)&&count($teams)>0)
#foreach($teams as $key=>$team)
<table>
<tr>
<th>id</th>
<th>Team Name</th>
<th>Payment</th>
<th>Time Remaining</th>
<th>Num of Players Paid</th>
<th>Paid out of</th>
<th>Captain Name</th>
<th>Status</th>
</tr>
<tr>
<td>{{$key+1}}</td>
<td>{{$team->name}}</td>
<td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td>
<td>
#if ($team->pivot->status == 0)
{{\Carbon\Carbon::createFromTimeStamp(strtotime($team->pivot->created_at.' +1 day'))->diffForHumans(null, true, true, 2)}}
#else
Registered at {{$team->pivot->updated_at->todatestring()}}
#endif
</td>
<td>{{ $team->competitionPayments->count() . '/' . $team->players->count() .' PAID'}}</td>
<td>
#foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
#endforeach
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
<td>{{$team->captain->full_name}}</td>
<td>{{$team->pivot->status}}</td>
{{-- <td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td> --}}
</tr>
</table>
<table>
<tr>
<th>Full Name</th>
<th>DOB</th>
<th>Active Kids Voucher AKV</th>
<th>Accept Reject AKV</th>
<th>Paid $</th>
<th>Paid By </th>
<th>Total</th>
<th>Stripe</th>
<th>Mobile</th>
<th>Email</th>
<th>T&C</th>
</tr>
#foreach ($team->players as $player)
<tr>
<td>{{ $player->full_name }}</td>
<td>{{ $player->dob }}</td>
<td>-</td>
<td>-</td>
<td>
{!! $player->competitionPayments->isNotEmpty() ?
implode($player->competitionPayments->map(function($item, $key) {
return ($key > 0 ? '<div class="mb-1"></div>' : '') . number_format($item->amount, 2) . ' AUD <br> <hr>' . $item->updated_at->todatestring();
})->toArray()) : '-' !!}
</td>
<td>{{ $player->competitionPayments->isNotEmpty() ? $player->competitionPayments->implode('paidBy.name') : '-' }}</td>
<td>-</td>
<td>-</td>
<td>{{ $player->mobile }}</td>
<td>{{ $player->email }}</td>
<td>-</td>
</tr>
#endforeach
</table>
<br><br><hr><br><br>
#endforeach
#else
<tr>
<td colspan="6">
<div class="text-muted small text-center">
No teams have registered yet
</div>
</td>
</tr>
#endif
Heres the relationship in my Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
I tried to add the sum() and get the total as below and i got a wrong total and repeated
<td>
#foreach ($team->competitionPayments as $amount)
{{ $amount->sum('amount') }}
#endforeach
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
This is the total i got
Someone please help me out or suggest a way that i could fix this issue
Thank You
Try array_sum() method. Replace this block
#foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
#endforeach
With this
#php
$temparr = (array) $team->competitionPayments
#endphp
{{array_sum(array_column($temparr,'amount'))}}
Or directly try
#php
$sum = 0
#endphp
#foreach ($team->competitionPayments as $amount)
$sum = $sum + $amount->amount
#endforeach

How to create dynamic rowspan in table in laravel

I am newbie in laravel, i'm trying to show the table like image below
https://i.stack.imgur.com/l52Vo.jpg
#foreach($report_data as $index => $item)
<tbody class="table-content">
<tr>
<td>{{ $index+1 }}</td>
<td style="text-align:left;">{{ $item->customer_fullName }}</td>
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
</tbody>
#endforeach
but the output is like image below
https://i.stack.imgur.com/rSktI.png
Thanks!!
I would do it as so:
In the controller, select the customers eager loading the vehicles.
$customers = Customer::with('vehicles')->get();
return view('customers')->with('customers', $customers);
In the view:
<table>
#foreach($customers as $index => $customer)
<tr>
<td rowspan="$customer->vehicles->count()">
{{ $index+1 }}
</td>
<td rowspan="$customer->vehicles->count()">
{{ $customer->fullName }}
</td>
<td rowspan="$customer->vehicles->count()">
{{ $customer->phone }}
</td>
<td> {{$customer->vehicles[0]->licensePlate }} </td>
<td> {{$customer->vehicles[0]->brandName }} </td>
<td> {{$customer->vehicles[0]->modelName }} </td>
</tr>
#for($i=1;$i<$customer->vehicles->count())
<tr>
<td> {{$customer->vehicles[$i]->licensePlate }} </td>
<td> {{$customer->vehicles[$i]->brandName }} </td>
<td> {{$customer->vehicles[$i]->modelName }} </td>
</tr>
#endfor
#endforeach
</table>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Customer</th>
<th scope="col">Licence Number</th>
<th scope="col">Brand</th>
<th scope="col">Brand Type</th>
</tr>
</thead>
<tbody>
#foreach($report_data as $index => $item)
<tbody class="table-content">
<tr>
<td>{{ $index+1 }}</td>
<td style="text-align:left;">{{ $item->customer_fullName }}</td>
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
</tbody>
#endforeach
</tbody>
use this CDN for Bootstrap style
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
You need not to iterate tbody everytime, just iterate <tr></tr> as below
<table>
<thead>
<tr>
<th>No</th>
<th>Customer</th>
<th>Phone</th>
<th>License Number</th>
<th>Brand</th>
<th>Brand Type</th>
</tr>
</thead>
<tbody class="table-content">
#foreach($report_data as $index => $item)
<tr>
<td>{{ $index+1 }}</td>
<td rowspan="2" style="text-align:left;">{{ $item->customer_fullName }}</td> //rowspan should be in some condition
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
#endforeach
</tbody>
</table>

Cannot get the list of the modes of an app

I can't do a foreach with my response
I already tried many things but nothing resolved it
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$client = new \GuzzleHttp\Client();
$effects = [];
$res = $client->request('GET', 'http://85.171.71.189/get_modes');
$effects = json_decode($res->getBody()->getContents(), true);
return view('base.effects', [ 'effects' => $effects ]);
}
#if($effects)
#foreach($effects as $effect)
<tr>
<td>{{ $effect['mode'] }}</td>
<td>{{ $effect['name'] }}</td>
<td><button class="btn btn-sm btn-primary">Activate</button></td>
<tr>
#endforeach
#else
<tr>
<td colspan="3">No effects found.</td>
</tr>
#endif
I expect that I have a table with all modes inside, but the actual error is
Undefined index: name (View: H:\wamp64\www\scintillement\resources\themes\light\base\effects.blade.php)
Try this:
#forelse ($effects as $effect)
<tr>
<td>{{ isset($effect['mode']) ? $effect['mode'] : '--' }}</td>
<td>{{ isset($effect['name'] ? $effect['name'] : '--' }}</td>
<td><button class="btn btn-sm btn-primary">Activate</button></td>
<tr>
#empty
<tr>
<td colspan="3">No effects found.</td>
</tr>
#endforelse
You should try this:
#if($effects)
#foreach($effects as $effect)
<tr>
#if(isset($effect['mode']))
<td>{{ $effect['mode'] }}</td>
#else
<td>-</td>
#endif
#if(isset($effect['name']))
<td>{{ $effect['name'] }}</td>
#else
<td>-</td>
#endif
<td><button class="btn btn-sm btn-primary">Activate</button></td>
<tr>
#endforeach
#else
<tr>
<td colspan="3">No effects found.</td>
</tr>
#endif
There were some empty rows in get from the http://85.171.71.189/get_modes
You can handle it by adding condition of empty or not before displaying in the table
#if($effects)
#foreach($effects as $effect)
#if(!empty($effect)) // new condition added
<tr>
<td>{{ $effect['mode'] }}</td>
<td>{{ $effect['name'] }}</td>
<td><button class="btn btn-sm btn-primary">Activate</button></td>
<tr>
#endif
#endforeach

A non-numeric value encountered in Laravel script

In my Laravel script on a page this error appears:
"A non-numeric value encountered (View: /home/grammer/public_html/test/core/resources/views/admin/apiServices.blade.php)"
for this line
<td>{{ $key+1 }}</td>
my page codes:
#extends('admin.layouts.master')
#section('page_icon', 'fa fa-suitcase')
#section('page_name', 'API Services')
#section('body')
<div class="row">
<div class="col-md-12">
<div class="tile">
<h3 class="tile-title">API Services List</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Service ID</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Min</th>
<th>Max</th>
</tr>
</thead>
<tbody>
#foreach($items as $key => $item)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ isset($item->service->id) ? $item->service->id : $item->service}}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->category }}</td>
<td>{{ isset($item->rate) ? $item->rate : $item->price_per_k }} $</td>
<td>{{ $item->min }}</td>
<td>{{ $item->max }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
Just add this :
<td>{{ (int) $key + 1 }}</td>
or Add this:
$key = (int) $key; // GET NUMBER FROM VARIABLE

Symfony2: If value equals something then change it to something else

I'm fetching and displaying data from a database, but data that means "NO" is represented by a "(", while data that means "YES" is represented by an "X". When I'm displaying the data in the twig, how do I convert the "(" to a "NO" and the "X" to a "YES"?
Here's my twig.html file:
{% extends 'base.html.twig' %}
{% block body %}
<div class="container">
<div class="starter-template">
<h1><strong>{{ shrub.commonname }}</strong></h1>
<p>THE FOLLOWING IS DETAILED information about the species <strong>{{ shrub.botanicalname }}</strong> (common name <strong>{{ shrub.commonname }})</strong>.</p>
{% if shrub == "(" %}
value=="YES"
<table class="table table-striped">
<hr>
<tbody>
<tr>
<th>ph Preference</th>
<td>{{ shrub.phpreference }}</td>
</tr>
<tr>
<th>Borderline Hardy</th>
<td>{{ shrub.borderlinehardy }}</td>
</tr>
<tr>
<th>Tolerates Wet Soil</th>
<td>{{ shrub.wetsoil }}</td>
</tr>
<tr>
<th>Tolerates Moist Soil</th>
<td>{{ shrub.moistsoil }}</td>
</tr>
<tr>
<th>Prefers Peaty Soil</th>
<td>{{ shrub.peatysoil }}</td>
</tr>
<tr>
<th>Prefers Well-drained Soil</th>
<td>{{ shrub.welldrainedsoil }}</td>
</tr>
<tr>
<th>Tolerates Drought</th>
<td>{{ shrub.drought }}</td>
</tr>
<tr>
<th>Tolerates Clay Soil</th>
<td>{{ shrub.claysoil }}</td>
</tr>
<tr>
<th>Prefers Sandy Soil</th>
<td>{{ shrub.sandysoil }}</td>
</tr>
<tr>
<th>Prefers Loam Soil</th>
<td>{{ shrub.loamsoil }}</td>
</tr>
<tr>
<th>Tolerates Infertile Soil</th>
<td>{{ shrub.infertilesoil }}</td>
</tr>
<tr>
<th>Prefers Rich Soil</th>
<td>{{ shrub.richsoil }}</td>
</tr>
<tr>
<th>Tolerates Compacted Soil</th>
<td>{{ shrub.compactedsoil }}</td>
</tr>
<tr>
<th>Tolerates City Conditions</th>
<td>{{ shrub.cityconditions }}</td>
</tr>
<tr>
<th>Tolerates Pollution</th>
<td>{{ shrub.pollution }}</td>
</tr>
<tr>
<th>Tolerates Salt Conditions</th>
<td>{{ shrub.salt }}</td>
</tr>
<tr>
<th>Tolerates Windy Conditions</th>
<td>{{ shrub.windy }}</td>
</tr>
<tr>
<th>Prefers Shade</th>
<td>{{ shrub.shade }}</td>
</tr>
<tr>
<th>Prefers Part Shade</th>
<td>{{ shrub.partshade }}</td>
</tr>
<tr>
<th>Prefers Full Sun</th>
<td>{{ shrub.fullsun }}</td>
</tr>
</tbody>
</table>
{% endif %}
<ul>
<li>
Back to the list
</li>
<li>
Edit
</li>
<li>
{{ form_start(delete_form) }}
<input type="submit" value="Delete">
{{ form_end(delete_form) }}
</li>
</ul>
</div>
</div>
{% endblock %}
And my controller if that matters:
public function showAction(Shrubs $shrub)
{
$deleteForm = $this->createDeleteForm($shrub);
return $this->render('shrubs/show.html.twig', array(
'shrub' => $shrub,
'delete_form' => $deleteForm->createView(),
));
}
To set a twig variable based on some logic, you need to use something like this.
{% set value = '' %}
{% if '(' == shrub %}
{% set value = 'YES' %}
{% elseif 'X' == shrub %}
{% set value = 'NO' %}
{% endif %}
I used a twig extension, like someone suggested above:
public function getFilters()
{
return array(
new \Twig_SimpleFilter('x', array($this, 'booleanFilter')),
);
}
public function booleanFilter()
{
$x = "yes";
return $x;
}
Then the variable: {{ shrub.wetsoil | x}}

Categories