I am having some trouble writing some nested HTML Tables from my Laravel Collection.
My Model
public static function offers($userId)
{
return DB::table('users_vehicles')
->join('dealer_offers', 'users_vehicles.id', '=', 'dealer_offers.vehicle_id')
->where('user_id', '=', $userId)
->select('users_vehicles.*', 'dealer_offers.*');
}
My Controller
public function dash()
{
$userCars = UserVehicles::offers(Auth::user()->id)->get();
return view('customer.dash', compact('userCars'));
}
My Collection Results
In My View blade Template
#foreach( $userCars->chunk(1) as $userCarChunk)
<div id="no-more-tables">
<table class="table table-bordered table-hover">
<caption>Offers for My Mazda 326 </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT </th>
<th class="numeric"> 3 DAYS To Accept </th>
</tr>
</thead>
<tbody>
#foreach( $userCarChunk as $car)
<tr>
<td data-title="First Name">
<div class="rating">
<label>
<input type="radio" name="rating" value="5" title="5 stars"> 5
</label>
</div>
</td>
<td data-title="Last Name"> 55 000 </td>
<td data-title="Username">
Accept
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endforeach
Front-End View
Question
How can I create a nested HTML Table for all the user vehicles, so that I can write the vehicle name and all the offers for that particular vehicle?
I hope my question is clear.
Try it like this :
<div id="no-more-tables">
<table class="table table-bordered table-hover">
<caption>Offers for My Mazda 326 </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT </th>
<th class="numeric"> 3 DAYS To Accept </th>
</tr>
</thead>
<tbody>
#foreach( $userCars->chunk(1) as $userCarChunk)
#foreach( $userCarChunk as $car)
<tr>
<td data-title="First Name">
<div class="rating">
<label>
<input type="radio" name="rating" value="5" title="5 stars"> 5
</label>
</div>
</td>
<td data-title="Last Name"> 55 000 </td>
<td data-title="Username">
Accept
</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
</div>
Answer
My Model UserVehicles.php
public function offers() {
return $this->hasMany(DealerOffers::class, 'vehicle_id');
}
My Controller CustomerController.php
$offersToCustomer = UserVehicles::with('offers')->get();
Dashboard Blade template
#foreach( $offersToCustomer as $offer)
<div id="no-more-tables_">
<table class="table table-bordered table-hover">
<caption>Offers for My :: <b style="color: #00BFF0"> {{ $offer->vehicle_make }} {{ $offer->vehicle_model }} </b> </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT REQUESTED </th>
<th class="numeric">AMOUNT OFFERED </th>
<th class="numeric"> Expires :: {{ Carbon\Carbon::parse($offer->offer_expiry)->format('d-m-Y') }} </th>
</tr>
</thead>
<tbody>
#foreach( $offer->offers as $subOffer)
<tr>
<td data-title="DEALER RATING">
<input id="rating" name="rating" class="rating rating-loading" value="3" data-min="0" data-max="5" data-step="1" />
</td>
<td data-title="AMOUNT REQUESTED">R {{ number_format($offer->vehicle_amount_asked, 2, '.', ' ') }} </td>
<td data-title="AMOUNT OFFERED">R {{ number_format($subOffer->offer_amount, 2, '.', ' ') }} </td>
<td data-title="ACTIONS">
<button data-offer_id="{{ $offer->id }} " data-vehicle_id="{{ $subOffer->vehicle_id }}"
class="btn btn-xs btn-success accept-offer">Accept</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endforeach
Output
Related
I want Make Different on Table, Each table has a service name Dontt Need Loop Again Table same Group Name, Each group has the name of the service
Check : https://i.ibb.co/NTnynGq/639.png
View : Package.blade.php
<div class="pt-120 pb-120">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="table-responsive--md">
<div class="services-table w-100">
<div class="service-group">
<div class="card">
#foreach($packages as $singlePackage)
<br>
<table class="table table-bordered style--two">
<thead>
<tr>
<th style="width:70%">
<span>Group Name : {{ $singlePackage->groups->name }}</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Action')</span>
</th>
</tr>
</thead>
<tbody>
<tr class="block item">
<td class="word-break">{{ $singlePackage->name }}</td>
<td>
<span>{{ $singlePackage->delivery_time }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
</tr>
</tbody>
</table>
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Package Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Package extends Model
{
protected $guarded = ['id'];
public function groups()
{
return $this->belongsTo(Group::class);
}
}
PackageController.php
public function packages()
{
$pageTitle = 'Packages';
$packages = Package::where('status', 1)->with('groups')->paginate(getPaginate());
return view('package',compact('pageTitle', 'packages'));
}
Want Separation each group has the name of the service
Option 1: You can add an ORDER BY group_id in the query
$packages = Package::where('status', 1)->with('groups')->orderBy('group_id')->paginate(getPaginate());
Option 2: You can sort by the group name in the foreach
#foreach($packages->sortBy(function ($item) { return $item->group->name; }) as $singlePackage)
or
#foreach($packages->sortBy(fn($item) => $item->group->name) as $singlePackage)
Option 3: Use the Collection's groupBy method.
<div class="card">
#foreach($packages->groupBy(function ($item) { return $item->groups->name; }) as $name => $group)
<br>
<table class="table table-bordered style--two">
<thead>
<tr>
<th style="width:70%">
<span>Group Name : {{ $name }}</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Action')</span>
</th>
</tr>
</thead>
<tbody>
#foreach($group as $singlePackage)
<tr class="block item">
<td class="word-break">{{ $singlePackage->name }}</td>
<td>
<span>{{ $singlePackage->delivery_time }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
</div>
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
please help me.
Trying to get property of non-object (View:
/Data/oa-dev4/resources/views/surat_tugas/list_surat_tugas/table_total.blade.php)
(View:
/Data/oa-dev4/resources/views/surat_tugas/list_surat_tugas/table_total.blade.php)
<div class="caption">
<i class="fa fa-table font-blue-oa"></i>
<span class="caption-subject font-blue-oa sbold uppercase">Rekap Biaya dan Pelaksanaan Surat Tugas Tahun {{$tahun_st}}</span>
</div>
<div class="portlet-body">
<table class="table table-datatable table-bordered table-checkable table-hover">
<thead>
<tr class="heading">
<th class='hidden'></th>
<th class="text-center"> </th>
<th class="text-center"> Jumlah Hari </th>
<th class="text-center"> Transport </th>
<th class="text-center"> Penginapan</th>
<th class="text-center"> Uang Harian</th>
<th class="text-center"> Jumlah</th>
</tr>
</thead>
<tbody>
<tr>
<td class='sbold'>Total</td>
#if($rekap->jumlah_hari)
<td class="text-right">{{$rekap->jumlah_hari}} Hari</td>
#else
<td class="text-right"> - </td>
#endif
#if(Helper::convert_rupiah($rekap->transport))
<td class="text-right">{{Helper::convert_number($rekap->transport)}}</td>
#else
<td class="text-right"> - </td>
#endif
#if(Helper::convert_rupiah($rekap->penginapan))
<td class="text-right">{{Helper::convert_number($rekap->penginapan)}}</td>
#else
<td class="text-right"> - </td>
#endif
#if(Helper::convert_rupiah($rekap->harian))
<td class="text-right">{{Helper::convert_number($rekap->harian)}}</td>
#else
<td class="text-right"> - </td>
#endif
#if(Helper::convert_rupiah($rekap->transport) || Helper::convert_rupiah($rekap->transport)|| Helper::convert_rupiah($rekap->transport))
<td class="text-right">{{Helper::convert_number($rekap->transport + $rekap->harian + $rekap->penginapan)}}</td>
#else
<td class="text-right"> - </td>
#endif
<tr>
</tbody>
</table>
</div>
The following error means you are accessing a value that is not an object.
Trying to get property of non-object
If the $rekap variable is an array then you have to convert it into an object and then access is in you blade template.
I have a table display and each row contains a button. With this button l have to pass the id to the controller where using this Admin can view user details one by one.
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<button type="submit" class="btn btn-
default" name="viewcv"> View</button> <br>
<br>
</td>
</tr>
#endforeach
</tbody>
</table>
Please check the code , hope it helps:
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>CV</td>
</tr>
#endforeach
</tbody>
</table>
Assuming you want to pass the field "id" or change it according to your requirement.
If you want to link to a show view, try something like this:
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<a href="{{action('JobController#show', $row->id)}}" class="btn btn-default">View</button>
</td>
</tr>
#endforeach
This will create a link to a view, where you can show the data.
In your controller JobController, you must create a function that receives the data:
public function show(Request $request, $id) {
...
}
Just pass the id of the record to controller function and get the details from database.
<td>
View <br>
</td>
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();