Laravel - Get data from another table (no relationship) and display on view - php

I'm new to laravel and I can't seem to display a data from another table that is not related. I have three tables, Department, Section and Position.
Department Has Many Section
Section Has Many Position
I want to display the department name from the Department table in the position view.
Position Controller
$getPositionWithSection = PositionModel::with('section')->get();
return view('PositionView.add', compact('getPositionWithSection'));
Position View HTML
#foreach($getPositionWithSection as $id => $employee)
<tr>
<td>{{ ++$id }}</td>
<td>{{ $employee->section->department_name }}</td>
<td>{{ $employee->section->section_name }}</td>
<td>{{ $employee->position_name }}</td>
</tr>
#endforeach
I was able to display the name of the section, but how do I display the name of the department?

#foreach($getPositionWithSection as $id => $employee)
<tr>
<td>{{ ++$id }}</td>
<td>{{ $employee->section->department->department_name}}</td>
<td>{{ $employee->section->section_name }}</td>
<td>{{ $employee->position_name }}</td>
</tr>
#endforeach

Related

Laravel sort two foreach loop

I have two foreach loops:
#foreach($masseurs as $masseur)
<tr>
<td>{{ $masseur->name }}</td>
<td>{{ $masseur->visa_expire }}</td>
</tr>
#endforeach
#foreach($masseurs as $masseur)
<tr>
<td>{{ $masseur->name }}</td>
<td>{{ $masseur->passsport_expire }}</td>
</tr>
#endforeach
I would like to sort the results of the two loops with sortBy, so I assume that I need to combine them. How to do that?
In Controller:
$masseurs->sortBy('visa_expire');
In view:
#foreach($masseurs as $masseur)
<tr>
<td>{{ $masseur->name }}</td>
<td>{{ $masseur->visa_expire }}</td>
<td>{{ $masseur->passsport_expire }}</td>
</tr>
#endforeach

Property [jenis_penyedia] does not exist on this collection instance

Property [jenis_penyedia] does not exist on this collection instance.
#php $no = 1; #endphp
#foreach($profile as $p)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $p->jenis->jenis_penyedia }}</td>
<td>{{ $p->nama }}</td>
<td>{{ $p->no_ktp }}</td>
<td>{{ $p->npwp }}</td>
<td>{{ $p->bank }}</td>
<td>{{ $p->no_rek }}</td>
<td>{{ $p->email }}</td>
<td>{{ $p->no_telp }}</td>
<td>{{ $p->keahlian }}</td>
<td>{{ $p->pengalaman }}</td>
<td>{{ $p->alamat }}</td>
<td>{{ $p->pendidikan }}</td>
<td>
Eager load your jenis to Profile.
$profile = Profile::with('jenis')->get();
$p->jenis is an Eloquent Collection. This Collection may contain many Jenis model instances (because of the hasMany relationship definition). Those models have the attribute jenis_penyedia, not the Collection.
You would need to decide which model you want to get that attribute of since there potentially are many, or none. Maybe using the first one works for your needs:
{{ $p->jenis->first()->jenis_penyedia ?? "some default value" }}

Generate a 2.500k pages PDF

I am inside a laravel 5.7 application, using the package barryvdh / laravel-snappy and trying everything (this code is the last attempt), to generate a dynamically PDF that has at least 2,000 pages.
This code is about my last attempt inside the view.
\DB::table(\DB::raw('`n_f_blocks`'))
->join('n_f_files', 'n_f_files.id', '=', 'n_f_blocks.n_f_file_id')
->where('n_f_files.group_id', '=', $file->id)
->where('tipo', 'saida')->orderBy('n_f_blocks.id', 'asc')->chunk(1000, function($data) {
foreach($data as $nf) :
#endphp
<tr>
<th scope="row">{{ $nf->nota }}</th>
<td>{{ $nf->chave }}</td>
<td>{{ date("d/m/Y", strtotime($nf->data)) }}</td>
<td>{{ $nf->mod }}</td>
<td>{{ $nf->ser }}</td>
<td>{{ $nf->bc_pis_original }}</td>
<td>{{ $nf->bc_cofins_original }}</td>
<td>{{ $nf->pis_original }}</td>
<td>{{ $nf->cofins_original }}</td>
<td>{{ $nf->icms_original }}</td>
<td>{{ $nf->bc_pis_processado }}</td>
<td>{{ $nf->bc_cofins_processado }}</td>
<td>{{ $nf->pis_processado }}</td>
<td>{{ $nf->cofins_processado }}</td>
<td>{{ $nf->icms_processado }}</td>
</tr>
#php
endforeach;
});
#endphp
It is possible?
I already received several errors, timeout, max memory size ..
I really do not know how to solve.

Obtaining field from table using primary key

Using Laravel and Revisionable Package for tracking changes. In my view I'm populating my table:
#foreach($revisions as $revision)
#if($revision->key == 'created_at' && !$revision->old_value)
<tr>
<td>{{ $revision->revisionable_type }}</td>
<td>{{ $revision->revisionable_id }}</td>
<td width="50">{{ $revision->userResponsible()->first_name }}</td>
<td Width="50"></td>
<td></td>
<td>{{ $revision->newValue() }}</td>
<td width="150">{{ $revision->created_at }}</td>
</tr>
#else
<tr>
<td>{{ $revision->revisionable_type }}</td>
<td>{{ $revision->revisionable_id }}</td>
<td width="50">{{ $revision->userResponsible()->first_name }}</td>
<td width="50">{{ $revision->fieldName() }}</td>
<td>{{ $revision->oldValue() }}</td>
<td>{{ $revision->newValue() }}</td>
<td width="150">{{ $revision->updated_at }}</td>
</tr>
#endif
#endforeach
The second column {{ $revision->revisionable_id }} happens to be the primary key of the user record that was modified. I would like to return the email address or first_name/last_name of that users record. I'm fairly new at this and could use a push in the right direction!
Thanks!
You can access the model that given revision relates to by accessing the revisionable relation of that revision. In your case, in order to display the email property of related model, you could do the following:
<td>{{ $revision->revisionable->email }}</td>
from the variable names revisionable_type & revisionable_id I assume you're using many to many Polymorphic relation. based on that
you've to define a relation in the Revision Model with the User like so:
public function users()
{
return $this->morphedByMany('App\User', 'taggable');
}
so you can use all the revisions related to this user $revision->users this will return array
and if you're sure that you've only one record related to this object like your case as I think. you can use $revision->users->first()->email or username, etc..
Laravel Docs

Laravel 4.2 DB returning all records

I am using Laravel 4.2 and catching single data from DB, because foreach loop is giving me last record all time
This is foreach in my blade.php file:
#foreach($vrataMreze as $vrataMrezeVar)
<tr>
<td>{{ $vrataMrezeVar->nalogBroj }}</td>
<td>{{ $vrataMrezeVar->nazivNaloga }}</td>
<td>{{ $vrataMrezeVar->narucitelj }}</td>
<td>{{ $vrataMrezeVar->datumIzrade }}</td>
<td>{{ $vrataMrezeVar->statusProizvodnje }}</td>
<td>{{ $vrataMrezeVar->datumOtpreme }}</td>
<td>{{ $vrataMrezeVar->nacinOtpreme }}</td>
<td>{{ $vrataMrezeVar->statusPoslovnice }}</td>
<td>{{ $vrataMrezeVar->datumMontaze }}</td>
<td>{{ $vrataMrezeVar->montirano }}</td>
<td>{{ $vrataMrezeVar->isporuceno }}</td>
<td align="center"><span class="glyphicon glyphicon-ok"></span></td>
<td align="center"><span class="glyphicon glyphicon-trash"></span></td>
<td align="center"><span class="glyphicon glyphicon-edit"></span></td>
<td align="center"></span></td>
</tr>
Route for this action:
Route::get('/nalozi/mreze/vrataMrezaOpen', array('uses' => 'MrezeController#openVrataMreza', 'as' => 'openVrataMreza'));
And function from my controller:
public function openVrataMreza($id)
{
$vrataMreze = DB::table('VrataMreza')->where('id', $id)->first();
return View::make('nalozi.Mreze.Vrata_Mreze.vrataMrezaOpen')->with('vrataMreze', $vrataMreze);
}
Also I tried with
Session::set('id', $vrataMrezeVar->id);
and
Session::get('id');
but didn't work, aslo get last record from database..
Your question is not much clear , but to get last record you can modify eloquent query.
public function openVrataMreza($id)
{
$vrataMreze = DB::table('VrataMreza')->where('id', $id)->orderBy('id','DESC')->first();
return View::make('nalozi.Mreze.Vrata_Mreze.vrataMrezaOpen')->with('vrataMreze', $vrataMreze);
}

Categories