Get row value in foreach in laravel view from query - laravel-5

I have to get the subject name of every row for the query newsubject = " ". the query required 4 parameters and 3 have been provided by an input. The subject is to be obtained from the view row. i want to be able to obtained the rank to subject with a class of student with certain academic year and programme
$getProgramme = Input::get('getProgramme');
$getYear = Input::get('getYear');
$getTerm = Input::get('getTerm');
$getLevel = Input::get('getLevel');
$getResults = DB::table('resultsView')
->leftjoin(DB::raw('((SELECT sregNumber1, scorelevel1,term_Desc1,term_Year1, newsubject,prog1,
CASE WHEN #prevRank = total_score1 THEN #curRank WHEN #prevRank := total_score1 THEN #curRank := #curRank + 1 END AS subject_ranked FROM scorebysubject q,
(SELECT #curRank :=0, #prevRank := NULL) r where prog1 ="' . $getProgramme . '" and `scorelevel1` = "' . $getLevel . '" and term_Desc1 = "' . $getTerm . '"
and term_Year1 = "' . $getYear . '" and newsubject = " " ORDER BY total_score1 Desc) as t3 )'), function ($join) {
$join->on('sregNumber1', '=', 'regNumber')
->on('newsubject', '=', 'subject')
->on('term_Desc1', '=', 'termDesc')
->on('prog1', '=', 'programmes')
->on('term_Year1', '=', 'ternYear');
})
->where('programmes', $getProgramme)
->where('ternYear', $getYear)
->where('termDesc', $getTerm)
->where('level', $getLevel)
->get();
this is my view
</tr>
</thead>
#foreach ($item->groupBy('groups') as $grouped)
<tr>
<td>
<td>{{$grouped[0]->groups}}</td>
</td>
</tr>
#foreach ($grouped as $row)
<tr>
<td>{{ isset($row->subject) ? $row->subject : '' }}</td>
<td>{{ $row->exams30 }}</td>
<td>{{ $row->exams70 }}</td>
<td>{{ $row->exams100 }}</td>
<td>{{ $row->subject_ranked }}</td>
<td>{{ $row->grade }}</td>
<td>{{ $row->remarks }}</td>
#endforeach
</tr>
#endforeach
#endforeach

Related

Value correlation from multiple tables in laravel

I have two tables. One employees and one timesheets
I would like to have correct name at the result but different ID. If you see the result I have the same name everywhere.
How can make the code to fixed my challenge?
Thank you.
In controller I have this code
$profile = ['no' => Auth::user()->no];
$timesheets = Timesheet::where($profile)->select('*')->orderBy('created_at','DESC')->get();
foreach ($timesheets as $employee) {
$emp = ['identification' => $employee->identification];
$oneemployee = Employees::where($emp)->select('*')->orderBy('created_at','DESC')->get();
}
return view('contractor.employees.timesheets', compact('timesheets', 'oneemployee'));
and in blade I have
#foreach ($timesheets as $row1)
<tr>
<td>#</td>
#foreach ($oneemployee as $row2)
<td>{{ $row2->fname}}</td>
<td>{{ $row2->lname}}</td>
#endforeach
<td>{{ $row1->identification}}</td>
<td>{{ $row1->week}}</td>
<td>{{ $row1->year}}</td>
</tr>
#endforeach
According to your commit, I suggest the following solution
If you want dispay timesheet with employees you can use eager loading.
For example:
In your controller:
$profile = ['no' => Auth::user()->no];
$timesheets = Timesheet::where($profile)->with("employees")->select('*')->orderBy('created_at','DESC')->get();
return view('contractor.employees.timesheets', compact('timesheets'));
In your Timesheet model add hasOne relation to Employee:
public function employee(){
return $this->hasOne(Employee::class,'identification','identification')
}
And your view:
#foreach ($timesheets as $timesheet)
<tr>
<td>#</td>
<td>{{ $timesheet->employee->fname}}</td>
<td>{{ $timesheet->employee->lname}}</td>
<td>{{ $timesheet->identification}}</td>
<td>{{ $timesheet->week}}</td>
<td>{{ $timesheet->year}}</td>
</tr>
#endforeach

i want to display the list of django many to many field not query set

i want to have the list of many to many field
this is my model
class ElectionType(models.Model):
name = models.CharField(max_length = 255)
class Election Name(models.Model):
name = models.CharField(max_length=200)
types = models.ManyToManyField(ElectionType)
view
def ElectionType(request):
ElectionType = ElectionName.objects.all()
context = {'election_type': election_type}
type.types is returning ElectionType.None
{% for type in election_type %}
<tr>
<td>{{ type.id }}</td>
<td>{{ type.name }}</td>
<td>{{ type.types }}</td>
<tr>

How to calculate using data from get() in controller

I have some data from a table and I'm doing an eloquent get for it. So far from what I know is when using get(), I need to loop it first using foreach to get the data.
My question is how to move my logic code to controller? so I just need to pass it to view. Because I'm doing it on blade directly
Bellow is my controller, I'm just simply get a data from database:
$attendances = Attendance::where('company_id', Auth::user()->company_id)->get();
#foreach ($attendances as $attendance)
// How to move this php code to my controller?
#php
$work_hour_start = $attendance->work_hour_start;
$attendance_time = \Carbon\Carbon::parse($attendance->attendance_time)->format('H:i:s');
$tolerance = \Carbon\Carbon::parse($attendance_time)->addMinutes($attendance->late_tolerance);
if ($tolerance > $work_hour_start) {
$is_late = 'Late';
} else {
$is_late = 'On Time';
}
#endphp
<tr>
<td>{{ $attendance->employee->name }}</td>
<td>{{ Carbon\Carbon::parse($attendance->attendance_time)->format('H:i:s') }}</td>
<td>{{ $attendance->location_library->location_name }}</td>
<td>{{ $is_late }}</td>
</tr>
#endforeach
I want my blade to be clean, but I don't know how to do that.
You could add attendance_time to the $casts property and make tolerance/is_late accessors in the Attendance Model
# Attendance.php
class Attendance extends Model
{
...
protected $casts = [
'attendance_time' => 'datetime',
];
...
public function getToleranceAttribute()
{
return $this->attendance_time->addMinutes($this->late_tolerance);
}
public function getIsLateAttribute()
{
return $this->tolerance > $this->work_hour_start
? 'Late'
: 'On time';
}
...
}
#foreach ($attendances as $attendance)
<tr>
<td>{{ $attendance->employee->name }}</td>
<td>{{ $attendance->attendance_time->format('H:i:s') }}</td>
<td>{{ $attendance->location_library->location_name }}</td>
<td>{{ $attendance->is_late }}</td>
</tr>
#endforeach
Just do it, move the logic to your controller and set it up in your $attendances collection. Something like this:
$attendances = Attendance::where('company_id', Auth::user()->company_id)->get();
foreach ($attendances as $key => $attendance){
$attendances[$key]->attendance_time = \Carbon\Carbon::parse($attendance->attendance_time)->format('H:i:s');
if (\Carbon\Carbon::parse($attendances[$key]->attendance_time)->addMinutes($attendance->late_tolerance > $attendance->work_hour_start) {
$attendances[$key]->setAttribute('is_late', 'Late');
} else {
$attendances[$key]->setAttribute('is_late', 'On Time');
}
}
return view('your-view', ['attendances' => $attendances]);
Now it will be able to iterate on your template:
#foreach ($attendances as $attendance)
<tr>
<td>{{ $attendance->employee->name }}</td>
<td>{{ $attendance->attendance_time }}</td>
<td>{{ $attendance->location_library->location_name }}</td>
<td>{{ $attendance->is_late }}</td>
</tr>
#endforeach

Property does not exist on this collection instance. (Laravel 6)

I have a for-loop that adds <td> rows on my table. Problem is, I cannot access the attributes of my Schedule model and it keeps giving me
Property [date_of_shift] does not exist on this collection instance.
<table class="table table-bordered table-responsive">
<thead>
<th>Employee</th>
<!-- Format of date_of_shift field: format('Y-m-d') -->
#for ($i = 0; $i < 7; $i++)
<th>
{{ \Carbon\Carbon::today()->addDays($i)->format('F j, Y') }}<br />
{{ \Carbon\Carbon::today()->addDays($i)->format('D') }}
</th>
#endfor
</thead>
<tbody>
#foreach ($employees as $employee)
<tr>
<td>{{ $employee->user->last_name }}, {{ $employee->user->first_name }} {{ $employee->user->maiden_name }} </td>
#for ($j = 0; $j < 7; $j++)
#if ( $employee->schedule->date_of_shift == \Carbon\Carbon::today()->addDays($j)->format('Y-m-d') )
<td>{{ $employee->schedule->time_of_shift }}</td>
#else
<td><strong>REST DAY</strong></td>
#endif
#endfor
</tr>
#endforeach
</tbody>
</table>
Employee.php
public function schedule () {
return $this->hasMany('App\Schedule');
}
Schedule.php
public function employee () {
return $this->belongsTo('App\Employee');
}
What is a workaround for this?
Since your relationship is HasMany the $employee->schedule returns an instance of collection so in your case the easiest thing you can do is to get ->first() result of the collection...
#if ( $employee->schedule->first()->date_of_shift == \Carbon\Carbon::today()->addDays($j)->format('Y-m-d') )
<td>{{ $employee->schedule->first()->time_of_shift }}</td>
#else
<td><strong>REST DAY</strong></td>
#endif
OR
Or don't change any code in your blade instead change your relation from HasMany to HasOne
public function schedule () {
return $this->hasOne('App\Schedule');
}
Employee.php
public function schedules () {
return $this->hasMany('App\Schedule');
}
Schedule.php
public function employees () {
return $this->belongsTo('App\Employee');
}
And According to this employee can have many schedule so you cant do like
#if ( $employee->schedules->date_of_shift == \Carbon\Carbon::today()->addDays($j)->format('Y-m-d') )
<td>{{ $employee->schedules->time_of_shift }}</td>
#else
You have to use #foreach if u want to check many schedules
You can try this
#foreach($employee->schedules as $schedule)
#if ( $schedule->date_of_shift == \Carbon\Carbon::today()->addDays($j)
>format('Y-m-d') )
<td>{{ $schedule->time_of_shift }}</td>
#else
#endforeach
But if u want the latest one you can just utilize first() method like this
#if ( $employee->schedule->first()->date_of_shift == \Carbon\Carbon::today()->addDays($j)
>format('Y-m-d') )
<td>{{ $employee->schedule->first()->time_of_shift }}</td>
#else
But in case of schedule . It belongs to one employee so you dont have to use foreach
Thanks . Let me know if it helps

Accessing further data

So I am accessing an external API and I am trying to display this data
https://i.stack.imgur.com/Yop0I.png
and everything was fine so far as you can see
https://i.stack.imgur.com/L3hXq.png
using this code
<tr v-for="cv_output in cv_outputs" :key="cv_output.id">
<td>{{ 'teste' }}</td>
<td>{{ cv_output['id'] }}</td>
<td>{{ cv_output['last-modified-date'] }}</td>
<td>{{ cv_output['output-category']['value'] }}</td>
<td>{{ cv_output['output-category']['code'] }}</td>
<td>{{ cv_output['output-type']['value'] }}</td>
<td>{{ cv_output['output-type']['code'] }}</td>
<td>{{ cv_output['other-output'] }}</td>
but as soon as I try to go further such as
{{ cv_output ['other-output']['title'] }}
the data stops displaying in the table and in the console i get the following:
TypeError: Cannot read property 'title' of null
which doesn't make any sense I think. Any idea why?
Controller method:
public function getRemoteOutputs()
{
$science = Auth::user()->science_id;
$client = new Client(['headers' => ['Accept' => 'application/json']]);
$request = $client->get(
'https://url_to_the_api/'.$science.'/degree',
[
'auth' => ['client', 'secret'],
]
);
$data = $request->getBody()->getContents();
return $data;
}
I #Helpinghand is correct, one of the records is missing other-output.
Try:
<td>
<span v-if="cv_output['output-type']">{{ cv_output['output-type']['title'] }}</span>
<span v-else>No Output Type Title</span>
</td>
Try this:
{{ cv_output ['other-output'] ? cv_output ['other-output']['title']: ''}}
This will return the other-output->title if it exists, if not it should return nothing without error. (you can put whatever you want in the single quotes there if you want something like 'not available' as default)

Resources