get results from one to one relationship - laravel-5

I make one to one relationship between two table. One for employees and other for their timesheet.
When I want to see the employee hours is not working ! So far I just enter hours through phpmyadmin.
public function up()
{
Schema::create('timesheets', function (Blueprint $table) {
$table->increments('id');
$table->integer('starz_id');
//starz = employee. I just name it starz
for($i=1;$i<=31;$i++){
$table->integer('Jan'.$i)->nullable();
}
$table->timestamps();
});
}
<table class="table table-bordered table-hover">
<tr>
<td>Name</td>
#for($i=1;$i<=14;$i++)
<td>Jan{{$i}}</td>
#endfor
<td>Total</td>
</tr>
<tr>
#foreach($timesheet as $one)
<td>
{{$one->starz->name}}
</td>
#for($i=1;$i<=14;$i++)
<td>
{{ $one->Jan.$i }}
</td>
#endfor
</tr>
#endforeach
</table>
The resutl
Name Jan1 Jan2 Jan3 Jan4 Jan5 Jan6 Jan7 Jan8 Jan9 Jan10 Jan11 Jan12 Jan13 Jan14 Total
ali 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Also I need to know please what is the right way to calculate the actual time. For ex: I want latter make function that caculate the actual hours working hours spent from start time (9am) and end time(6.30pm). How can I do this by laravel ?

i think what you want to achieve could have been done in other terms and it was gonna be easier for u. Allow me to write something for you
Employee.php
public class Employee{
public timesheets(){
return $this->hasMany(TimeSheet::class, 'starz_id');
}
}
TimeSheet.php
public class TimeSheet{
public employee(){
return $this->belongsTo(TimeSheet::class, 'starz_id');
}
}
timesheet migration
Schema::create('timesheets', function (Blueprint $table) {
$table->increments('id');
$table->integer('starz_id');
//now from your form i will encourage you log in an employee using their entry and exit time
$table->string('date')->nullable();
$table->string('entry_time')->nullable();
$table->string('exit_time')->nullable;
$table->timestamps();
});
with this you can use PHP to calculate the time worked maybe using Carbon library.
on rendering the employee time sheet you can improvise using some other logic. Looks like looping is you favorite
Your logic above will work but its not every month that has 31 days

Related

How I car write Controller function on many to many with relation attributes

I am working on applying many to many relations on table Accident, Car where the relations are as follows:
In the Accident model:
public function car()
{
return $this->belongsToMany(Car::class,'accident_participateds',
'reportNo','vehicleId');
}
In-car Model
public function accidents()
{
return $this->belongsToMany(Accident::class,'accident_participateds',
'vehicleId','reportNo')->withPivot(['damageAmount', 'IBAN']);
}
I want to display the data from all 3 Models (Car, Accident,accident_participateds)My question is whether these statements are correct in the controller
public function AdminClaims()
{
$claim = Accident::with('car')->get();
$details = Car::with('accidentsr')->get();
return view('admin.AdminClaims',compact('claim','details'));
}
Because when I try to display like this:
<tbody>
#foreach($claim as $claims)
<tr>
<td>{{$claims->ReportNumber}}</td>
#foreach($details as $AP)
<td>{{$AP->vehicleId}}</td>
<td>{{$AP->Location}}</td>
<td>{{$AP->Date}}</td>
<td>{{$AP->damageAmount}}</td>
<td>{{$AP->IBAN}}</td>
#endforeach
</tr>
#endforeach
it print only ReportNumber
kindly tell me where I made a mistake?
From what I understand, you want to display the details of Accident(claim), associated Car and data from the pivot table.
To do so you can try
public function AdminClaims()
{
$claims = Car::with('accidents')->get();
return view('admin.AdminClaims',compact('claims'));
}
And in view
#foreach($claims as $claim)
<tr>
<td>{{$claim->ReportNumber}}</td>
<td>{{$claim->vehicleId}}</td>
<td>{{$claim->Location}}</td>
<td>{{$claim->Date}}</td>
<td>{{$claim->pivot->damageAmount}}</td>
<td>{{$claim->pivot->IBAN}}</td>
</tr>
#endforeach

Fetching a string of IDS in a Column using explode (Eloquent model Laravel)

For the table Exam I have this:
--id----section_id
--1----- 2,3
when I use this in Exam.php Model:
public function Sections(){
return $this->belongsTo(Section::class, 'section_id');
}
It only shows and take in consideration the section_id=2 but not both of the IDS.
How I can get Info from both Ids ?
in the blade.php :
#foreach ($Exams as $exams)
<tr>
<td>{{$exams->id}}</td>
<td>{{$exams->Sections}}</td>
</tr>
#endforeach

Laravel #foreach Repeating Same Data

when I am trying to add #foreach 3 times it's repeating my data. how do I solve this?
and am using Livewire.
Small Summery :
I will try to make a Student Management system using Laravel and livewire. basically, I am trying to make a payment system based on Monthly, Termly, and Year so it's also separate to more. Example We have Different price Value of Monthly Payment Scheme Students.
this is my code
public function mount($students){
$students = Student::With('FeeScheem','FeeScheemInd','FeeGroup','FeeMaster','FeeCollector')
->where('students.stu_id',$students )
->get();
$this->students = $students;
// DD($students);
}
Eloquent Relationship
public function FeeScheem()
{
return $this->hasOne(FeeScheem::class, 'fs_id', 'stu_fee_scheems');
}
public function FeeScheemInd()
{
return $this->hasOne(FeeScheemInd::class, 'ind_id', 'stu_ind_fee_scheems');
}
public function FeeGroup()
{
return $this->hasMany(FeeGroup::class, 'fee_group_fee_scheem_id', 'stu_fee_scheems');
}
public function FeeMaster()
{
return $this->hasOne(FeeMaster::class, 'fee_master_ind_fs_id', 'stu_ind_fee_scheems');
}
public function FeeCollector()
{
return $this->hasMany(SchoolFeeCollector::class, 'fc_stu_id', 'stu_id');
}
This is My Blade File
<tbody>
#foreach($students as $Student)
#foreach($Student->FeeGroup as $FeeG)
#foreach($Student->FeeCollector as $FeeC)
<tr>
<td></td>
<td>{{$FeeG->fee_group_name}}</td>
<td>{{$FeeG->fee_master_due_date}}</td>
<td></td>
<td>{{$Student->FeeMaster->fee_master_amount}}</td>
</tr>
#endforeach
#endforeach
#endforeach
</tbody>
Here the table view in Blade file

show all data from parent and child table

I have two simple one to many relationship tables of medicines and bills. as it happens, many medicines are bought by a single bill.
Now I have a page to show all the bills in each row of a table. Also, I want to be able to view all the sold medicines related to each bill by clicking on the respective bill and the medicines will show up below the row using JQuery.
This is my view code to depict all the bills in a table:
<tbody>
#foreach($bills as $bill)
<tr role="row" class="odd">
<td class="v-align-middle semi-bold">{{$bill->id}}</td>
<td class="v-align-middle semi-bold">{{$bill->customername}}</td>
<td class="v-align-middle">{{$bill->paid}}</td>
<td class="v-align-middle">{{$bill->remainders}}</td>
<td class="v-align-middle">{{$bill->total}}</td>
<td class="v-align-middle">{{$bill->created_at}}</td>
</tr>
#endforeach
</tbody>
and my controller code is:
public function index()
{
$bill = Billpc::all();
return view('bills')->with('bills', $bill);
}
Can anyone help me with how to show all the sold medicines related to the respective bill?
add relationships in your model
Bill model
public function medicines()
{
return $this->hasMany('App\Submission');
}
Medicine model:
public function bill()
{
return $this->belongsTo('App\AssignSubmission');
}
then change your code in your controller. use with to get the medicines in your bill
Controller
public function index()
{
$bill = Billpc::with('medicines')->get();
return view('bills')->with('bills', $bill);
}

Laravel — Loop all data from multiple tables

Good day. I have researched about it, but nothings matched with my problem. Here's the scenario. I have courses table, and 3 more tables, assignments, quizzes, and reports. Now, what I want is, to get all of the records from the table assignments, quizzes, and reports and loop through all the records below a specific course.
Example Output: The order should be according to the first created item.
Course1
Assignment1
Quiz1
Quiz2
Assignment2
Report1
How can I do that with three different tables? I know how to use the basic many to many relationship, but for this. I really got stacked. Need help guys.
Note: I'm using Laravel5.1
Update1 — Course.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $fillable = [];
public function assignments(){
return $this->belongsToMany('Assignment');
}
public function quizzes(){
return $this->belongsToMany('Quiz');
}
public function reports(){
return $this->belongsToMany('Report');
}
}
UPDATE2 as per request
<div class="table">
<table class="tabled">
<thead>
<tr>
<th>x</th>
</tr>
</thead>
<tbody>
#foreach($course_items as $course_item)
<tr>
<td>
{{ $table_name }} {{ $course_item->name }}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Based on the limited information available from your question, you will need to merge your relations into a single collection.
Using unions would be a better solution, if feasible, to improve efficiency, but hey-ho.
// Prepare not deleted scope
// I would suggest using Laravel's SoftDeletes, as it'll be better than writing your own implementation. But if not, you would be better to create this as a reusable scope on your model
$notDeletedScope = function ($query) {
return $query->where('deleted', 0);
};
// Find course with non-deleted relations
$course = Course::with([
'assignments' => $notDeletedScope,
'reports' => $notDeletedScope,
'quizzes' => $notDeletedScope,
])
->findOrFail($id);
// Combine relations into single collection
$merged = collect($course->assignments);
$merged = $merged->merge(collect($course->quizzes));
$merged = $merged->merge(collect($course->reports));
// Sort merged relations by created_at
$merged = $merged->sortBy('created_at');
#foreach ($merged as $relation)
#if ($relation->type === 'assignment')
// ...
#elseif ($relation->type === 'quiz')
// ...
#elseif ($relation->type === 'report')
// ...
#endif
#endforeach
Add to each model:
public function getTypeAttribute()
{
return snake_case(substr(strrchr(get_class($this), '\\'), 1));
}

Resources