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
Related
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
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)
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.
I have a for each loop to iterate over the collection of records. It has the user_id, but not the other user credentials. I created another collection of user. How do I include this in my iteration?
Controller
public function index(Request $request, $id)
{
$thoughts = Thought::where('id', $id)->orderBy('created_at', 'desc')->paginate(100)->get();
$user = User::where('id', $thoughts->user_id);
return view('backend.pages.thought_list', compact('thoughts, user'));
}
View
#foreach ($thoughts as $thought)
<tr>
<td class="col-md-1">{{ $user->username}} <span class="user_id_thought">ID - {{ $user->user_id}}</span></td>
<td class="col-md-1">{{ $thought->response }}</td>
<td>{{ $thought->thought_type }}</td>
<td>{{ $thought->id }}</td>
</tr>
#endforeach
How do I display my users variable in the loop. Or is there a better way of doing this.
Eager Loading is the perfect use case for your requirement in my opinion.
e.g.
$thoughts = Thought::with('user')->get();
foreach ($thoughts as $thought) {
echo $thought->user->name;
}
For more details see: https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
I have a problem with show one to one relationship in Laravel.
I have that tables in DB:
manufacturers with defaul Laravel id column and cashregisters with manufacturer_id column releated to id from manufacturers table.
In models I do that:
Cashregister
class Cashregister extends Model
{
protected $table = "cashregisters";
public function manufacturer(){
return $this->hasOne('App\Manufacturer','id');
}
}
Manufacturer
class Manufacturer extends Model
{
protected $table = "manufacturers";
public function cashregisters(){
return $this->belongsTo('App\Cashregister');
}
}
I think this is ok. Next I do in CashregisterController that in index():
$cashregisters = Cashregister::all();
return view('cashregisters.index')->withCashregisters($cashregisters);
In cashregisters index View i do that loop:
<tbody>
#foreach ($cashregisters as $cashregister)
<tr>
<th>{{ $cashregister->id }}</th>
<td>{{ $cashregister->manufacturer_id }}</td>
......
<td>{{ $cashregister->DataNastPrzegUrzFisk }}</td>
#endforeach
</tr>
</tbody>
And it works great. Show everything from cashregisters table.
I want to display Manufacturer name instead of manufacturer_id, so I try do this that:
<td>{{ $cashregister->manufacturer->name }}</td>
Bu I got an error.
Next I searchover internet and try do that thing
#foreach ($cashregisters as $cashregister)
<tr>
<th>{{ $cashregister->id }}</th>
#foreach ($cashregister->manufacturer() as $item)
<td>{{ $item->name}}</td>
#endforeach
......
<td>{{ $cashregister->DataNastPrzegUrzFisk }}</td>
#endforeach
</tr>
</tbody>
But this piece of code show nothing :( Can anyone help me to fix that. When I after all table do that code:
<td>{{ $cashregister->manufacturer->name}}</td>
it show some information, so I think relationship is ok.
You need to set local key for manufacturer, like this:
class Cashregister extends Model
{
protected $table = "cashregisters";
public function manufacturer(){
return $this->hasOne('App\Manufacturer','id', 'manufacturer_id');
}
}
Use with() to load the relation:
$cashregisters = Cashregister::with('manufacturer')->all();
Then this will show the manufacturer:
$cashregister->manufacturer->name