How to display this unserialize data to blade in Laravel - laravel

public function getcustomerorder($id)
{
$orderss = Orders::find($id);
$orders = $orderss->all();
$orders->transform(function($order){
$order->cart = unserialize($order->cart);
return $order;
});
return $orders;
}
The output of that from the controller is below:
[{"id":3,"cart":{"1":{"itemcode":"20062872","itemname":"AXE BS TWIST 50ML","quantity":4,"price":125},"2":{"itemcode":"20062881","itemname":"DOVE RO ORIGINAL 40ML","quantity":5,"price":50}},"firstname":"Joseph Vincent","lastname":"Limbaroc","phonenumber":"09197963942","email":"joseph.coquilla#outlook.com","street":"094 B Brgy. 40-D Bolton Ext. St.","street2":null,"city":"Davao City","province":"Davao Del Sur","zip":"8000","status":"new","created_at":"2020-04-20T05:15:09.000000Z","updated_at":"2020-04-20T05:15:09.000000Z"}]
How do I display the array of cart into blade using
#foreach($orders as $item)
<tr>
<td>{{ }}</td>
</tr>
#endforeach

Hi Sarvil you could change the structure of your JSON a little. You can make "cart" to be an array not an object but I hope this syntax will help you:
#foreach($orders as $item)
<tr>
<td>{{$item->id}}</td>
</tr>
#foreach($item->cart as $item_cart)
<tr>
<td>{{$item_cart->itemcode}}</td>
</tr>
<tr>
<td>{{$item_cart->itemname}}</td>
</tr>
#endforeach
<tr>
<td>{{$item->firstname}}</td>
</tr>
#endforeach

Related

How to sum table row data in laravel 8

I want to sum up the data in my table. Please tell, How can I do it. I have tried the below method but it is not working.
This is my controller code
public function CreateRentCertificateReport(Request $request)
{
$data['reports'] = Report::distric()->status(1)->get();
return view('adc.reports.start-create-report', $data);
}
This is my view code
<tbody>
#foreach($reports as $data)
<tr>
<td>{{$data->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$data->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
#foreach($reports as $data)
<td>{{$data['column_one']->countBy()}}</td>
#endforeach
</tr>
</tbody>
You can use pluck() and sum() :
<tbody>
#foreach($reports as $data)
<tr>
<td>{{ $data->upazila->upazila_name }}</td>
<td class="numeric_bangla">{{ $data->column_one }}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ $reports->pluck('column_one')->sum() }}</td>
</tr>
</tbody>
Maybe by doing something like this:
#php
$total = 0;
foreach($reports as $report) {
$total += $report['column_one']
}
#endphp
<tbody>
#foreach($reports as $report)
<tr>
<td>{{$report->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$report->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{$total}}</td>
</tr>
</tbody>
Instead of calling ->get() on an Eloquent query, you can call ->sum(“column_name”) to get a number which is the sum of that column’s values

Laravel #foreach Repeating

when I am trying to add #foreach 3 times it's repeating my data. how do I solve this?
this is my code
My Controller
$students = Student::with('FeeScheem','IndFeeScheem','FeeGroup','FeeGroup1')
->where('students.stu_id', $students)
->get();
$this->students = $students;
Eloquent Relationship
public function FeeScheem()
{
return $this->hasOne(FeeScheem::class, 'fs_id', 'stu_fee_scheem_id');
}
public function IndFeeScheem()
{
return $this->hasOne(IndividuallyFeeScheem::class, 'id', 'stu_ind_fee_scheem_id');
}
public function FeeGroup()
{
return $this->hasManyThrough(FeeMaster::class, FeeGroups::class);
}
public function FeeGroup1()
{
return $this->hasMany(FeeGroups::class, 'individually_fee_scheem_id', 'stu_fee_scheem_id');
}
My Blade File
#foreach ($students as $stu=>$student)
#foreach ($student->FeeGroup as $key=>$FeeG)
#foreach ($student->FeeGroup1 as $key1=>$FeeG1)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $FeeG1->fee_group_name }}</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
#endforeach
#endforeach
#endforeach
</tbody>
This is a preview
Click here to preview
You can use the index property of the loop variable :
#foreach ($students as $stu=>$student)
#if($loop->index < 2)
// Your code
#endif
#endforeach
you closed the marker incorrectly. should be something like:
#foreach ($students as $stu=>$student)
#foreach ($student->FeeGroup as $key=>$FeeG)
<tr>
<td></td>
</tr>
#endforeach
#foreach ($student->FeeGroup1 as $key1=>$FeeG1)
<tr>
<td>{{ $FeeG1->fee_group_name }}</td>
<td></td>
</tr>
#endforeach
#endforeach
</tbody>

How to fetch value from multiple table through pivot table in Laravel?

I'm trying to fetch value from different tables. I get the value but it repeat same quantity. I'm getting this see this image
I expect this see this image
Here is my controller code
$orders = Order::all();
view('admin.order.index', compact('orders'));
Here is my model relationship
return $this->belongsToMany(FoodItem::class, 'order_food_items', 'order_id', 'food_item_id')->withTimestamps();
}
public function orderItems() {
return $this->hasMany(OrderFoodItem::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Here is in blade code, May be I'm doing wrong
#foreach ($orders as $key => $order)
#foreach ($order->orderFoodItems as $product)
#foreach ($order->orderFoodItemPrice as $price)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $product->name }}</td>
<td>
<img src="{{ asset('/storage/items/food/' . $product->image) }}"
alt="">
</td>
<td>{{ $order->user->name }}</td>
<td>
<span class="badge badge-primary">Pending</span></td>
<td>something</td>
<td>{{ $price->discounted_price }}</td>
</tr>
#endforeach
#endforeach
#endforeach
Can anyone help me?

Cannot use object of type stdClass as array (View:

I want to use data 2 query in 1 view but
Error Cannot use object of type stdClass as array
(View:
public function adtranspc(Request $request)
{
$writter = Writter::all();
$processmaster = DB::table('rocessmaster')
->where('pcm_bname', 'LIKE', "%Social%")
->get();
return view('processspj.adtranspc',[
'writter' => $writter,
'processmaster' => $processmaster
]};
*This is my view (this error here)
<table id="table2" class="table table-hover table-striped">
<tr>
<th scope="col"></th>
<th scope="col">Colname1</th>
</tr>
#foreach ($processmaster as $item)
<tr>
<td>{{ $item['pcm_id'] }}</td>
<td>{{ $item['pcm_bname'] }}</td>
</tr>
#endforeach
</table>
This is my controller
public function adtranspc(Request $request)
{
$writter = Writter::all();
$processmaster = DB::table('rocessmaster')
->where('pcm_bname', 'LIKE', "%Social%")
->get();
return view('processspj.adtranspc',[
'writter' => $writter,
'processmaster' => $processmaster
]};
}
You are trying to print your object values as an array.You need to update your code with this, access your object values like this way
#foreach ($processmaster as $item)
<tr>
<td>{{ $item->pcm_id }}</td>
<td>{{ $item->pcm_bname }}</td>
</tr>
#endforeac

Print indexed array value in view.blade.php in laravel5.2

I have one associative array $collection and one indexed array $gnd which I have passed from Controller to my view.blade.php in Laravel 5.2. I want to print the values of both the arrays in a single table. Here is my code,
<table class="responsive-table highlight centered">
<thead>
<tr>
<th data-field="id">Sl.no</th>
<th data-field="name">Unique </th>
<th>Name</th>
<th data-field="price">Description</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{{-- */$j = 1;/* --}}
#foreach($collection as $key=>$value)
<tr>
<td>{{ $j }}</td>
<td>{{ $value->uid }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->desc }}</td>
<td>{{ $gnd[$j] }}</td>
{{-- */$j++;/* --}}
#endforeach
</tr>
</tbody>
</table>
For {{ $gnd[$j] }} I am getting the following error.
ErrorException in b7c1ab515a44988c31e1982a3ce014434e97ef2c.php line 30:
Undefined offset: 22 (View: /var/www/html/anudip/resources/views/plugins/entries/view.blade.php)
I am new in laravel. Please help me...
Function that passes the two arrays from Controller:
public function getDetails(){
$collection = DB::table('entry_transactions')
->leftJoin('entry_masters', 'entry_transactions.entry_id', '=', 'entry_masters.id')
->get(['entry_masters.id as uid','entry_masters.name as name','entry_masters.gender as gender','entry_transactions.entry_desc as desc']);
$gnd = array();
$len = sizeof($gnd);
$i = 0;
foreach ($collection as $key) {
if($key->gender == 0){
$gnd[$i] = "Male";
}
else {
$gnd[$i] = "Female";
}
$i++;
}
return view($this->url.'entries.view', compact('collection','gnd'));
}
The error you are getting because both array's size are not equal!
And why are you starting $j = 1? shouldn't be $j = 0, if both your arrays has the same size $j = 0 will fix your problem and change <td>{{ $j }}</td> to <td>{{ $j+1 }}</td>
Also, can you show us your controller function that is sending this two arrays?

Resources