Golang function call on template fails on a single struct - go

i have following code
func (w *Warehouse) GetId() string {
return w.Id.Hex()
}
view multiple works perfect
{{ range .Data }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .City }}</td>
<td>{{ .Manager }}</td>
<td><a class="blue" href="/warehouse/show/{{ .GetId }}">view</a></td>
</tr>
{{ end }}
in single show warehouse i do this but that fails while .Data.Name return the name correctly
{{ .Data.GetId }}
any idea what i am missing here?

problem is that i needed to make instance of my struct like this
var warehouse = new(models.Warehouse) // works
instead of
var warehouse models.Warehouse // fails

According to your code .Data is array or slice. It does not have GetId() method

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

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)

Can't display mongodb collection to view Laravel

I'm using MongoDb and Laravel for a demo.
I'm trying to get the collection from MongoDb and display it to view in Laravel.
Here is my model:
class Account extends Eloquent
{
public function getAllAccount() {
$account = DB::connection('mongodb')->collection('Account')->get();
return $account;
}
}
Here is my Controller:
class AccountController extends Controller
{
public function index() {
$acc = new Account();
$data = $acc -> getAllAccount();
return view('account', $data);
}
}
And then, I display it to view:
#if(isset($data))
#foreach($data as $dataValue)
<tr>
<td>{{ $dataValue->_id }}</td>
<td>{{ $dataValue->avail_balance }}</td>
<td>{{ $dataValue->last_activity_date }}</td>
<td>{{ $dataValue->open_date }}</td>
<td>{{ $dataValue->pending_balance }}</td>
<td>{{ $dataValue->cust_id }}</td>
<td>{{ $dataValue->open_branch_id }}</td>
<td>{{ $dataValue->open_emp_id }}</td>
<td>{{ $dataValue->product_id }}</td>
</tr>
#endforeach
#endif
I set Controller in web.php:
Route::get('/account', 'AccountController#index');
But when I run this, it return nothing, just a blank page.
How I can fix that?
Thank you very much!
UPDATE:
I use dd($data) in my controller and here is all my collection:
You've got to send the data back in a format the page can take.
In the index() method, change:
return view('account', $data);
to
return view('account', compact ('data');
To be pretty, you can use ->with() as well, but I like compact since you can move a lot of variables easily.

Resources