How to combine html table row with 1 reference and multiple items? - laravel

I am wondering if its possible to merge rows with same reference id into one but still display its data.
I am using Laravel and Datatables. Any idea?
From this:
To this:
I am using laravel.
here is my mysql query if it helps:
create temporary table purchasemgmnt
SELECT item_id,item_name FROM astramc.pm_employee_benefits where isactive = 'Yes'
UNION ALL
SELECT item_id,item_name FROM astramc.pm_furn_fixture where isactive = 'Yes'
UNION ALL
SELECT item_id,item_name FROM astramc.pm_ir_supplies where isactive = 'Yes'
UNION ALL
SELECT item_id,item_name FROM astramc.pm_marketing where isactive = 'Yes'
UNION ALL
SELECT item_id,item_name FROM astramc.pm_office_equipments where isactive = 'Yes'
UNION ALL
SELECT item_id,item_name FROM astramc.pm_office_supplies where isactive = 'Yes';
create temporary table purchaserequest
select
ppr.pr_id,
ppri.item_id,
pm.item_name,
ppr.remarks,
ppr.pr_status,
ppr.date_created
from pr_purchase_request as ppr
left join pr_purchase_request_information as ppri
on ppr.pr_id = ppri.pr_id
left join purchasemgmnt as pm on ppri.item_id = pm.item_id order by pr_id asc;
SET #row_number = 0;
select
#row_number:=#row_number + 1 AS rownum,
pr_id,
item_id,
item_name,
remarks,
pr_status,
date_created
from purchaserequest order by pr_id asc;
and my laravel view:
#foreach ($pmrequests as $pmrequest)
<tr>
<td>{{$pmrequest->rownum}}</td>
<td>{{$pmrequest->pr_id}}</td>
<td>{{$pmrequest->item_id}}</td>
<td>{{$pmrequest->item_name}}</td>
<td>{{$pmrequest->remarks}}</td>
<td>{{$pmrequest->date_created}}</td>
<td>{{$pmrequest->pr_status}}</td>
</tr>
#endforeach

Scenario:
Model PurchaseRequestHeader - underlying database table pr_header has many PurchaseRequestDetail - underlying database table pr_Details
Define both way relationship in the Model classes
class PurchaseRequestHeader extends Model
{
protected $table = 'pr_header';
public function details()
{
return $this->hasMany(PurchaseRequestDetails::class, 'pr_id', 'pr_id');
}
}
class PurchaseRequest Detail extends Model
{
protected $table = 'pr_details';
public function header()
{
return $this->belongsTo(PurchaseRequestHeader::class, 'pr_id', 'pr_id');
}
}
Once relationships are defined, we can retrieve all the PurchaseRequestDetail records related/associated with a record of PurchaseRequestHeader.
$data = PurchaseRequestHeader::with('details')->paginate(25);
$data will have PurchaseRequestHeader(s) with related PurchaseRequestDetails which can be rendered on the view
<tbody>
#foreach($data as $header)
<tr>
<td>{{ $header->pr_id }}</td>
<td>
<table><tbody>
#foreach($header->details as $detail)
<tr>
<td> {{ $detail->field1 }}</td>
<td> {{ $detail->field2 }}</td>
<td> {{ $detail->field3 }}</td>
<td> {{ $detail->field4 }}</td>
</tr>
#endforeach
</tbody></table>
</td>
</tr>
#endforeach
</tbody>

Related

Laravel 9 Calling oldestOfMany() in Controller from Model

I have a Customer model and Customer hasMany Xray_machines. I am trying to select the oldest cert_date from the xray_machines table to populate a table in the view that lists the customer_name and the oldest cert_date. I found oldestOfMany() and it seems to be a good option but it is displaying the first cert_date rather than the oldest.
Customer Model
public function xray_machine()
{
return $this->hasMany(Xray_machine::class);
}
public function oldestCert(){
return $this->hasOne(Xray_machine::class)->oldestOfMany('cert_date','min');
}
Controller
public function xray_unscheduled(){
$due = Carbon::now()->addMonth(3)->endOfMonth()->toDateTimeString();
$customers = Customer::whereHas('Xray_machine', function ($query) use($due) {
$query->where('cert_date','<=', $due);
})
->whereNull('xray_scheduled')->orWhere('xray_scheduled','<>','')
->oldestCert()
->orderBy(
Xray_machine::select('cert_date')
->whereColumn('xray_machines.customer_id','customers.id')
->take(1),'asc'
)
->get()->all();
return view('work_orders/xray_unscheduled', compact('customers'));
}
View
#foreach ($customers as $customer)
<tr>
<td>
{{ $customer->customer_name}}
</td>
<td>
{{ \Carbon\Carbon::parse($customer->oldestCert()->cert_date)->format('Y-m-d')}}
</td>
</tr>
#endforeach

Laravel - Using Where Clause with If Statement and Parameter

I have a Laravel Query with where clause statement and a parameter in it. The $plan coming from the source is string and the values are:
Daily, Weekly, Monthly, Wallet
but the value of $plan in the destination, users table ($users) are:
1,2,3,4
Parameter Source:
#foreach($billings as $key => $billing)
<tr>
<td>{{ ++$key }}</td>
<td><a class="btn btn-info" href="{{ route('revenueDetail',$billing->plan) }}">{{ $billing->plan }}</a></td>
<td>{{ $billing->total_plans }}</td>
<td>{{ $billing->total_amount }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $billings->links() }}
</td>
</tr>
Controller: Query
public function revenueDetail($plan = null)
{
$revenuedetails = DB::table("users")
->select(
"users.username",
DB::raw("DATE(users.created_at) as subscription_date")
)
->where('users.plan', $plan)
->get();
}
What I want to achieve is that, in the where clause in revenueDetail() function, if:
$plan = Daily then users.plan = 1
$plan = Weekly then users.plan = 2
$plan = Monthly then users.plan = 3
$plan = Wallet then users.plan = 4
How do I achieve this?
Based on last sentences, i assume you need to map Daily string which == 1
protected static $type = [
'Daily' => 1,
'Weekly' => 2,
'Montly' => 3,
]
then
public function revenueDetail($plan = null)
{
$revenuedetails = DB::table("users")
->select("users.username", DB::raw("DATE(users.created_at) as subscription_date"))
->where('users.plan', self::$type[$plan] ?? null) // ?? will escape exception if $plan is null
->get();
}
From view user should send id instead of value. However if u want to send value you should ask plans table for id of value u send. I recommend to store plans in cache. Another solution is to make class with Constans like your plans (if u dont want to extend it in the future)

How i get the name of user in this foreach

This is my controller
$user_rank = Sprintbacklog::select('assign_user_id',DB::raw('count(*) as
jumlah_data'))->where('id_sprint',$id)->where('finish',1)-
>groupBy('assign_user_id')->get();
This is foreach in blade
#foreach($user_rank as $name_and_finish)
<tr>
<td>{{ $name_and_finish->assign_user_id}}</td>
<td><span class="badge">{{ $name_and_finish->jumlah_data }}</span></td>
</tr>
#endforeach
maybe you must join the models, to access the user like
$user_rank = Sprintbacklog::join('users',sprintbacklogs.id_user,'=','users.id')
->where('id_sprint',$id)->where('finish',1)
->select('assign_user_id',DB::raw('count(*) as jumlah_data'))
->groupBy('assign_user_id')
->get();
to access the name in user table, but you need an user id at the table that you want to join with.

Unable to display relations data from eloquent relationship

I'm trying to display in my view data from an eloquent relationship but i seem to be doing a tiny bit wrong. dd shows the relation in the collection but i just can't call the data correctly in my view. Below is what i have done
Employee model
public function task()
{
return $this->hasMany(Task::class);
}
Task model
public function employee()
{
return $this->belongsTo(Employee::class);
}
TaskController
public function index()
{
$alltask = Task::with('employee')->get();
dd($alltask);
/*return view('task.task', compact('alltask', 'empwithtask'));*/
}
task view
#foreach ($alltask as $task)
<tr>
<td>{{ $task->priority }}</td>
<td>{{ $task->firstname }}</td>
/* this is meant to be the employee.firstname */
<td>{{ $task->title }}</td>
<td>{{ $task->begin }}</td>
</tr>
#endforeach
I'm not able to display $task->firstname, firstname is from the employees table. Below is a snapshot of the result of dd
How do I show the employee firstname?
$task->employee->firstname

Join 2 tables to a Pivot table LARAVEL 4.2

How can i show the classes only for student who is enrolled in a class and i have 2 tables and a pivot table?
Table 1: Home_Students : home_id , home_studid ....
Table 2: Hw_Classes :class_id , class_desc , class_date ....
Pivot table:Hw_StudentClasses :Stclass_id, Stclass_classid, Stclass_studid
So i made a model MySeminarClasses to communicate with the table Hw_StudentClasses and a Controller MySeminarClassesController
I made relations in the model of the other tables belongsToMany
public function Users(){
return $this->belongsToMany('User','home_id');
}
public function SeminarClass(){
return $this->belongsToMany('SeminarClass','class_id');
}
Also in the Controller i did this which im not so sure if its right but i did this from the instructions of the laravel 4.2 documentation
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->orderBy('class_date',strtotime('-4 month'))
->get();
Finally in the blade
<tbody>
#foreach($myclasses as $i=>$myclass)
<tr>
<td>{{ $i+1 }}</td>
**<td>{{ link_to_route('class.show',$myclass->Class_desc,$myclass->Class_id) }}</td>**
<td class="text-center">{{ date('j-n-Y G:i',strtotime($myclass->class_date)) }}</td>
<td class="text-center">{{ UserEnroll::where('Stclass_classid',$myclass->Class_id)->count() }}</td>
</tr>
#endforeach
</tbody>
After a 16 hours of struggle ......i aswered my own question ! This query with Auth::user helped me. Show the classes only for every user who is logged in
$id = Auth::user()->home_studid;
$date = date('m-d-Y', strtotime('-4 month'));
$seminars = Seminar::where('Package_Display', 1)
->orWhere('Package_Display', 2)
->orderBy('Package_Name', 'asc')
->get();
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->where('Home_Students.home_studid','=',$id)
->orderBy('class_date',strtotime('-4 month'))
->get();
HOPE THIS HELPS SOMEONE ! ^_^

Resources