Adding if else statement inside multiple foreach loop table - php

I have a table that loops its value from the database using FOREACH LOOP. The first loop seems okay when I inserted IF ELSE STATEMENT in another FOREACH LOOP inside the first loop, the td begin to not align anymore. Which means if the value is empty, it will hidden the cell and other values from the first loop will be push forward.
For your information, I'm using different server database which are SQL and MYSQL. SQL is my main db and mysql is for inserting, updating and deleting the textarea known as 'NOTES' in the table.
Here's the query from my controller
$getNote1 = DB::connection('mysql')->table('note1')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
$getNote2 = DB::connection('mysql')->table('note2')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
$getNote3 = DB::connection('mysql')->table('note3')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
$getNote4 = DB::connection('mysql')->table('note4')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
return view('posts.index')->with('title', $title)->with('query', $query)->with('getNote1', $getNote1)->with('getNote2', $getNote2)->with('getNote3', $getNote3)->with('getNote4', $getNote4);
Here's my table code
<table id="table1" class="table table-bordered">
<thead>
<tr>
<th style="display:none;">Stock Code</th>
<th>Stock Name</th>
<th>Note 1</th>
<th>Note 2</th>
<th>Note 3</th>
<th>Note 4</th>
<th>Order Level</th>
<th>Qty Level</th>
<th>Balance</th>
<th>Purchase Price</th>
<th>UOM</th>
#for ($i=0; $i<=12; $i++)
<th><?php echo date('Y/m', strtotime('-'.$i.' month', strtotime($getDate)));?></th>
#endfor
</tr>
</thead>
<tbody>
<!-- Foreach loop from db -->
#foreach ($query as $val)
<?php
$stockcode = $val->StockCode;
$stockname = $val->StockName;
$currentbalance = $val->CurrentBalance;
$purchaseprice = $val->PurchasePrice;
$baseuom = $val->BaseUOM;
?>
<tr>
<td style="display:none;">{{ $val->StockCode }}</td>
<td class="text-nowrap align-middle">{{ $val->StockName }}</td>
#foreach($getNote1 as $note1)
#if($note1->stockcode == $stockcode)
<td><textarea rows="2" cols="10" name="note1" class="note1 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}">{{ $note1->note }}</textarea></td>
#else
<td><textarea rows="2" cols="10" name="note1" class="note1 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea><td>
#endif
#endforeach
<td><textarea rows="2" cols="10" name="note2" class="note2 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea></td>
<td><textarea rows="2" cols="10" name="note3" class="note3 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea></td>
<td><textarea rows="2" cols="10" name="note4" class="note4 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea></td>
<td><input type="text" id="orderlevel" class="text-right"></td>
<td><input type="text" id="qtylevel" class="text-right"></td>
<td class="text-right align-middle">{{ number_format($currentbalance, 2) }}</td>
<td class="text-right align-middle">{{ number_format($purchaseprice, 2) }}</td>
<td class="text-center align-middle">{{ $baseuom }}</td>
#for ($i=1; $i<=13; $i++)
<?php
//looping variables
$Qty = 'Qty'.$i;
$Date = 'Date'.$i;
?>
<td class="text-right align-middle">
#if($val->$Qty > 0)
<a id="viewdetails" data-toggle="modal"
data-productid="{{$val->Id}}"
data-productname="{{$val->StockName}}"
data-getdate="{{date('Y-m', strtotime($val->$Date))}}"
href="#" data-target="#dataModal">{{ number_format($val->$Qty, 2) }}</a>
#endif
</td>
#endfor
</tr>
#endforeach
<!-- End Foreach loop -->
</tbody>
</table>
Here's my screenshot
Here's the table that I want it to be

don't hide the cell instead put an empty value in it.
<td><textarea rows="2" cols="10" name="note1" class="note1 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}">
#foreach($getNote1 as $note1)
#if($note1->stockcode == $stockcode)
{{ $note1->note }}
#endif
#endforeach
</textarea></td>
then it will align just fine

Related

How I can avoid this error on DOMPDF with laravel 8 when working with large tables?

I am working with DOMPDF in Laravel 8, when I create a table with many records and jumps to the next page, it does not perform the page break as it should, Could someone help me to know what I am doing wrong?. I was reading many articles on the internet and none of them worked for me.
Here is my code:
<div class="box-products-table">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td class="text-center roboto">{{ trans('image') }}</td>
<td class="text-center roboto">{{ trans('just_name') }}</td>
<td class="text-center roboto">{{ trans('quantity') }}</td>
<td class="text-center roboto">{{ trans('unit_price') }}</td>
<td class="text-center roboto">{{ trans('taxes') }}</td>
<td class="text-center roboto">{{ trans('subtotal') }}</td>
<td class="text-center roboto">{{ trans('total_price') }}</td>
</tr>
#for($i = 0; $i < 100; $i++)
<tr>
<td class="text-center"><img src="{{ public_path('storage/uploads/products/d6d74f351d482bb41787e86350ace02e.jpg') }}" height="70px"></td>
<td class="text-center">PORTATIL DELL 4HWRT</td>
<td class="text-right">526</td>
<td class="text-right">$ 4.985.650</td>
<td class="text-right">$ 947.273,5</td>
<td class="text-right">$ 99.713.000</td>
<td class="text-right">$ 2.622.451.900</td>
</tr>
#endfor
</tbody>
</table>
</div>
Result:
Go to screenshot (https://ibb.co/Rj5ZPTW)
.page_break {
page-break-before: always;
}
<!-- adjust $n as needed. $n = number of rows that fit in a page -->
#foreach(collect(range(1,100))->chunk($n) as $chunk)
<div class="box-products-table">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td class="text-center roboto">{{ trans('image') }}</td>
<td class="text-center roboto">{{ trans('just_name') }}</td>
<td class="text-center roboto">{{ trans('quantity') }}</td>
<td class="text-center roboto">{{ trans('unit_price') }}</td>
<td class="text-center roboto">{{ trans('taxes') }}</td>
<td class="text-center roboto">{{ trans('subtotal') }}</td>
<td class="text-center roboto">{{ trans('total_price') }}</td>
</tr>
#foreach($chunk as $i)
<tr>
<td class="text-center"><img src="{{ public_path('storage/uploads/products/d6d74f351d482bb41787e86350ace02e.jpg') }}" height="70px"></td>
<td class="text-center">PORTATIL DELL 4HWRT</td>
<td class="text-right">526</td>
<td class="text-right">$ 4.985.650</td>
<td class="text-right">$ 947.273,5</td>
<td class="text-right">$ 99.713.000</td>
<td class="text-right">$ 2.622.451.900</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="page-break"></div> <!-- break page -->
#endforeach

Trying to get property 'name' of non-object (View: C:\xampp\htdocs\AppLaravel\resources\views\admin\editBill.blade.php)

I need to go to my bill list page but this error is stopping me: 'Trying to get property 'name' of non-object'
My route:
Route::get('bill/{id?}',[
'as'=>'bill',
'uses'=>'PageController#editBill'
]);
My Controller:
public function editBill($id)
{
$customerInfo=DB::table('customer')->select('customer.id','customer.name','customer.created_at','customer.phone_number','customer.address','customer.note','bills.total','customer.email')->join('bills','bills.id_customer','=','customer.id')->where('customer.id', '=', $id)->first();
$billInfo=Bill::where('id',$id)->get();
return view('admin.editBill',compact('customerInfo','billInfo'));
}
My view:
<section class="content">
<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<div class="row">
<div class="col-md-12">
<div class="container123 col-md-6" style="">
<h4></h4>
<table class="table table-bordered">
<thead>
<tr>
<th class="col-md-4">Customer Info</th>
<th class="col-md-6"></th>
</tr>
</thead>
<tbody>
<tr>
<td>Customer name</td>
<td>{{ $customerInfo->name }}</td>
</tr>
<tr>
<td>Date Order</td>
<td>{{ $customerInfo->created_at }}</td>
</tr>
<tr>
<td>Phone Number</td>
<td>{{ $customerInfo->phone_number }}</td>
</tr>
<tr>
<td>Address</td>
<td>{{ $customerInfo->address }}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $customerInfo->email }}</td>
</tr>
<tr>
<td>Note</td>
<td>{{ $customerInfo->note }}</td>
</tr>
</tbody>
</table>
</div>
<table id="myTable" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info">
<thead>
<tr role="row">
<th class="sorting col-md-1" >Id</th>
<th class="sorting_asc col-md-4">Product name</th>
<th class="sorting col-md-2">Quantity</th>
<th class="sorting col-md-2">Unit_price</th>
</thead>
<tbody>
#foreach($billInfo as $key=>$bill)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $bill->product }}</td>
<td>{{ $bill->quantity }}</td>
<td>{{ number_format($bill->unit_price) }} VNĐ</td>
</tr>
#endforeach
<tr>
<td colspan="3"><b>Total</b></td>
<td colspan="1"><b class="text-red">{{ number_format($customerInfo->total) }} đ</b></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-12">
<form action="{{route('pdf',$customerInfo->id)}}" method="GET">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="col-md-8"></div>
<div class="col-md-4">
<div class="form-inline">
<label>Delivery Status: </label>
<select name="status" class="form-control input-inline" style="width: 200px">
<option value="1">None</option>
<option value="2">On going</option>
<option value="2">Delivered</option>
</select>
<input type="submit" method="get" value="Print" class="btn btn-primary">
</div>
</div>
</form>
</div>
</div>
</div>
</section>
The bill detail list is working fine but the customers detail is not
I tried others way like {{$customer->name ?? ''}} and it did't give an error anymore but it doesn't show any data

Converting multiple same name into one single name in laravel

This is controller part
$users_list = User::where('id', '>', 4)
->where('status', 1)
->orderBy('id')
->get();
$companys = WorkFor::all();
$categories = TaskCetagorys::all();
//$report_table = array();
foreach($users_list as $user){
$report_table = array();
$user_tasks = Task::select('task_date',DB::raw('group_concat(tasks.workfor_name) as workfor_name'),'taskcategory_name',DB::raw('group_concat(tasks.description SEPARATOR ",,,") as description'))
->wherebetween('task_date', array("$start_task_date", "$end_task_date"))
->where('asign_id', '=', $user->id)
->where('status', 1)
->groupBy('workfor_id')
->orderBy('task_date')
->get();
}
This is my view part
<table id = "datatable" class="table table-striped table-bordered" style="font-size: 85%">
<thead>
<tr>
<th class="bg-primary col-sm-2" >
<font size="4">
Employee
</font>
</th>
{{--<th class="bg-danger">--}}
{{--<font size="4">--}}
{{--Company--}}
{{--</font>--}}
{{--</th>--}}
{{--<th class="bg-success">--}}
{{--<font size="4">--}}
{{--Category--}}
{{--</font>--}}
{{--</th>--}}
{{--<th class="bg-warning">--}}
{{--<font size="4">--}}
{{--Task--}}
{{--</font>--}}
{{--</th>--}}
</tr>
</thead>
<tbody>
#foreach ($report_table_all as $report)
<?php
$name = $report[0]['user_name'];
$user_designation = $report[0]['user_designation'];
?>
<tr>
<td>
{{--<font size="2">--}}
<strong>{{$name}}</strong></br>
{{--</font>--}}
<font size="1">
{{$user_designation}}
</font>
</td>
<td>
#foreach ( $report as $daily_report )
<table class="table table-striped table-bordered datatable" style="font-size: 85%">
<thead>
<tr class="primary" style="font-size: 15px;">
<th class="bg-info">Concern</th>
<th class="bg-warning">Category</th>
<th class="bg-success">Task</th>
<th class="bg-danger">Date</th>
</tr>
</thead>
<?php
$total = 0;
$sn = 1;
$sn_total = '*';
?>
#foreach($daily_report['user_task_lists'] as $user_task)
<tr>
<td style="background-color: rgba(227, 255, 140, 0.98); ">
<center>{{$user_task->workfor_name}}</center>
</td>
<td>
<table class="table table-striped table-bordered datatable" style="font-size: 85%">
<tr>
<td style="font-size: 13px;background-color: rgba(19, 214, 255, 0.98)">
{{$sn}}.{{ $user_task->taskcategory_name }}
<?php $sn += floatval(1) ?>
</td>
</tr>
</table>
</td>
<td style="font-size: 12px;">
{{ $user_task->description}}
</td>
<td>
{{date('d-M-Y',strtotime($user_task->task_date))}}
</td>
</tr>
#endforeach
</table>
</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I am using laravel 5.2 version.in my controller i fetch one column data using group_concat function.When i showing these data using view,the same name repeats many times in a row.but i want to show these repeated data only one time.for example,i fetched my workfor_name from database using group_concat.then when i showimg them the workfor_name repeats many times as PALO,PALO,PALO,PALO,PALO,PALO,PALO,PALO,PALO in a row.i want only one PALO in my row.need help.!!!
this is my view page
Add the keyword DISTINCT to your GROUP_CONCAT call.
$user_tasks = Task::select('task_date',DB::raw('group_concat(DISTINCT tasks.workfor_name) as workfor_name'),'taskcategory_name',DB::raw('group_concat(DISTINCT tasks.description SEPARATOR ",,,") as description'))
->wherebetween('task_date', array("$start_task_date", "$end_task_date"))
->where('asign_id', '=', $user->id)
->where('status', 1)
->groupBy('workfor_id')
->orderBy('task_date')
->get();

Laravel & laravel-dompdf page breaking shows only 1st page

Trying to create a PDF invoice using barryvdh's laravel dompdf wrapper. I want the invoice to begin on a new page when a certain number of rows is reached. However I'm unsure on how to do it since I don't get the expected results. Here is my controller:
function invoice() {
$resellerId = Input::get('resellerId');
$startdate = Input::get('invoiceStartDate');
$enddate = Input::get('invoiceEndDate');
$orders = \App\order::where('reseller',1)->orderBy('date', 'desc')->where('customer_id' , $resellerId )->where('date', '>=',$startdate)->where('date', '<=',$enddate)->get();
$pdf = PDF::loadView('pdf.invoicePrint',['orders' => $orders]);
// $PDFOutput = $pdf->output();
return $pdf->stream("trestaki.pdf");
}
And here is the relevant portion of the blade:
<table style="border:1px solid #000;width: 100%;">
<tr>
<td>
<span style="font-size: 14px;font-weight: 900;">Art. Nr.</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Benämning</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Antal</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Pris</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Summa</span>
</td></tr>
<?php $i=0; ?>
#foreach($orders as $order)
<tr>
<td>{{ $order->order_id }}</td>
<td>{{ $order->getProduct->mobile }} {{ $order->getProduct->model }}</td>
<td>1</td>
<td>{{ $order->price }}</td>
<td>{{ $order->price }}</td>
</tr>
<?php
$i++;
if($i % 25 == 0)
echo "<div style='page-break-after: always;'></div>";
?>
#endforeach
<tr>
<td colspan=5 style="padding-top: 20px;">
<table style="width: 45%;">
<tr>
<td>
<span style="font-size: 14px;font-weight: 900;">Moms %</span>
</td>
<td>
<span style="font-size: 14px;font-weight: 900;">Netto</span>
</td>
<td>
<span style="font-size: 14px;font-weight: 900;">Moms</span>
</td>
<td>
<span style="font-size: 14px;font-weight: 900;">Summa</span>
</td>
</tr>
<tr>
<td>25%</td>
<td>639,20</td>
<td>159,80</td>
<td>799,00</td>
</tr>
</table>
</td>
</tr>
</table>
The thing is that if I remove all the closing tags from the echo, the webpage times out.
The problem was the fixed position of the main div. After removing it the page did break correctly.

Update multiple data from <select> <option> in database using laravel 5.1

i have some coding, i wanna to update multiple data from select option value in database using laravel 5.1. i'm using ajax onchange="form.submit()", so i can update data without submit.
this is my view
{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
<table class="dataTable" id="table-user">
<thead class="grey lighten-3">
<tr>
<td class="center-align no-sort">Photo</td>
<td class="center-align">Fullname</td>
<td class="center-align">Rule</td>
<td class="center-align">Action</td>
</tr>
</thead>
<tbody>
<?php $hitung = $UserAccess->count(); ?>
#foreach($UserAccess as $list)
<tr>
<td class="center-align" width="50">
#if($list->avatar == NULL)
<img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
#else
<img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
#endif
</td>
<td class="red1-text lato-bold center-align">{{$list->name}}</td>
<td class="center-align" width="150">
<select id="selectrole" name="selectrole" onchange="form.submit()">
#foreach($UserAccessRole as $listRole)
<option value="{{$listRole->id}}" #if($list->user_access_role_id_fk == $listRole->id) selected="selected"#endif>{{$listRole->name}}</option>
#endforeach
</select>
</td>
<input type = "hidden" value = "{{$list->id}}" name = "idmain">
#if($hitung > 2)
<td class="center-align">
<a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
</td>
#else
<td class="center-align">Remove Access</td>
#endif
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
this is my controller
public function doUpdateAccessRole(Request $request)
{
$UserAccess = UserAccess::orderBy('name', 'asc')->get();
$id_main = $request->input('idmain');
$id_role = $request->input('selectrole');
$Role = UserAccess::findOrFail($id_main);
$Role->user_access_role_id_fk = $id_role;
$Role->update($request->all());
return redirect('setting/useraccess');
}
this is my route
Route::post('setting/updaterole',['uses'=>'SettingController#doUpdateAccessRole','as'=>'updateaccessrole']);
using my coding update function already work, but just last id has been update. please help me, thank you
my code already work. just add foreach in my controller. like this, my controller :
public function doUpdateAccessRole(Request $request)
{
$UserAccess = UserAccess::all();
foreach($UserAccess as $list)
{
$id_main = $request->input('idmain'.$list->id);
$id_role = $request->input('selectrole'.$list->id);
$Role = UserAccess::find($id_main);
$Role->user_access_role_id_fk = $id_role;
$Role->update($request->all());
}
return redirect('setting/useraccess');
}
and this my view :
{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
<table class="dataTable" id="table-user">
<thead class="grey lighten-3">
<tr>
<td class="center-align no-sort">Photo</td>
<td class="center-align">Fullname</td>
<td class="center-align">Rule</td>
<td class="center-align">Action</td>
</tr>
</thead>
<tbody>
<?php $hitung = $UserAccess->count(); ?>
#foreach($UserAccess as $list)
<tr>
<td class="center-align" width="50">
#if($list->avatar == NULL)
<img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
#else
<img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
#endif
</td>
<td class="red1-text lato-bold center-align">{{$list->name}}</td>
<td class="center-align" width="150">
<select id="selectrole" name="selectrole{{$list->id}}" onchange="form.submit()">
#foreach($UserAccessRole as $listRole)
<option value="{{$listRole->id}}" #if($list->user_access_role_id_fk == $listRole->id) selected="selected"#endif>{{$listRole->name}}</option>
#endforeach
</select>
</td>
<input type = "hidden" value = "{{$list->id}}" name = "idmain{{$list->id}}">
#if($hitung > 2)
<td class="center-align">
<a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
</td>
#else
<td class="center-align">Remove Access</td>
#endif
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
and now my coding already work.

Categories