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
Related
I am new to Laravel and I am trying to get value of input with selected element's id.
When I am trying to get input value of quantity in controller it's actually empty.
Here is my code.
collect.blade.php
#foreach($reagentcard as $r)
<tr>
<td>{{ $i++ }}</td>
<td>{{ $r->casnumber }}</td>
<td>{{ $r->name }}</td>
<td>{{ $r->description }}</td>
<td>{{ $r->classification }}</td>
<td>{{ $r->manname }}</td>
<td>{{ $r->incomedate}} </td>
<td>{{ $r->expirationdate}} </td>
<td>{{ $r->quantity }} </td>
<td><input class="form-control" type="number" name="quantity"></td>
#if($message = Session::get('info'))
<strong style="color:red">{{ $message }}</strong><br>
#endif
#if($message = Session::get('error'))
<strong style="color:red">{{ $message }}</strong><br>
#endif
<td><button> Collect </button> </td>
#endforeach
controller.php
public function collectReagent($id, Request $request)
{
$user_id = Auth::user()->id;
$takedate = Carbon::now();
$reagentcard = ReagentCard::where('id',$id)->first();
$test = $request->route('quantity');
return view('test',compact('test', 'id'));
}
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>
I need help with a foreach loop in laravel. For some reason I need to have two rows instead of two tables (would have solved my problem) However, I have problems with it getting to work due to not knowing where to place my tags to get the right table structure.
Code:
<table class="minimalistBlack">
<tbody>
<tr>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
<th>X</th>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
</tr>
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
#foreach ($data2 as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
#endforeach
</tbody>
</table>
How can I combine the two foreach loops and and get the right table structure?
to achieve just that result you could do
foreach (array_combine($courses, $sections) as $course => $section)
but that only works for two arrays
or u can use two table and make feel as single table
<div class="row">
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
Assuming the $data and $data2 variables are Laravel collection instances, you can simply use $data = $data->concat($data2). Now $data will contain data of both variables. For a simple array, you can use $data += $data2;. This will merge $data & $data2 to $data. Then you can simply use:
...
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach
</tbody>
</table>
User array_merge function in controller like this
$array1 = array("john","ny");
$array2 = array("mitchel","ny");
$result = array_merge($a1,$a2);
if $data and $data2 have one, one row you can try like this.
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
</tr>
#endforeach
#foreach ($data2 as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach
I want to sum tot_Price and unit_Price respectively so that I can analyze the average of these two attributes.
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
How should I do this?
Thanks.
You could add sum variable for both tot_Price and unit_Price and add in foreach loop
<?php $sum_tot_Price = 0 ?>
<?php $sum_unit_Price = 0 ?>
#foreach ($yourData as X_land)
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
<?php $sum_tot_Price += $X_land->tot_Price ?>
<?php $sum_unit_Price += $X_land->unit_Price ?>
#endforeach
<tr>
<td>Sum tot_Price {{ $sum_tot_Price}}</td>
<td>Sum unit_Price {{ $sum_unit_Price}}</td>
</tr>
Assume:
#foreach($lands as $X_land)
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
#endforeach
You can get total like this:
{{$lands->sum('tot_Price')}}
{{$lands->sum('unit_Price')}}
Or you can get average:
{{$lands->avg('tot_Price')}}
{{$lands->avg('unit_Price')}}
To be very simple,
You need to focus on the #foreach loop first,
e.g #foreach($people as $person)
and there is the salary which you can access in foreach,
Through $person->salary
If you want to calculate the sum of salaries then use a simple sum like this,
$people->sum('salary'), Remember not use $person
In your case
{{$lands->sum('tot_Price')}}
{{$lands->sum('unit_Price')}}
I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach