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
Related
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" }}
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
I want to obtain the data of an Audit model, that within its columns old_values and new_values are stored arrays, but dynamic. When I do the foreach in the view it gives me the following error when wanting to show these columns:
ErrorException (E_ERROR) htmlspecialchars() expects parameter 1 to be string, array given (View: H:\DAF\resources\views\audit\index.blade.php)
I've already searched several blogs and they say how to do something similar but with static arrays, not with dynamic ones.
The Audit model is Laravel's vendor to audit called OwenIt\Auditing.
class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
{
use \OwenIt\Auditing\Audit;
/**
* {#inheritdoc}
*/
protected $guarded = [];
/**
* {#inheritdoc}
*/
protected $casts = [
'old_values' => 'json',
'new_values' => 'json',
'auditable_id' => 'integer',
];
}
Controller
<?php
namespace App\Http\Controllers;
use OwenIt\Auditing\Models\Audit;
class EstaticasController extends Controller {
public function audit() {
$audit = Audit::orderBy( 'id', 'DESC' )->get();
return view( 'audit.index', compact( 'audit' ) );
}
}
Vista
<table class="table table-bordered table-hover table-stripped">
<thead>
<tr>
<th>No</th>
<th>Operación</th>
<th>Tupla</th>
<th>Tabla</th>
<th>Valores antiguos</th>
<th>Valores Actuales</th>
<th>URL</th>
<th>IP</th>
<th>Creado</th>
<th>Actualizado</th>
</tr>
</thead>
<tbody>
<?php $no = 1 ?>
#foreach($audit as $item)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $item->event }}</td>
<td>{{ $item->auditable_id }}</td>
<td>{{ $item->auditable_type }}</td>
<td>{{ $item->old_values }}</td>
<td>{{ $item->new_values }}</td>
<td>{{ $item->url }}</td>
<td>{{ $item->ip_address }}</td>
<td>{{ $item->created_at }}</td>
<td>{{ $item->updated_at }}</td>
</tr>
#endforeach
</tbody>
</table>
Image of Table
Image of DB with data
I think here is the issue:
// ...
// <td>{{ $item->auditable_type }}</td>
<td>{{ $item->old_values }}</td>
<td>{{ $item->new_values }}</td>
// <td>{{ $item->url }}</td>
// ...
The problem here is that, in order to print them in screen, the front-end is expecting the value to be string, but you are giving it an array. That's why the error is throw:
ErrorException (E_ERROR) htmlspecialchars() expects parameter 1 to be string, array given (View: H:\DAF\resources\views\audit\index.blade.php)
To solve it, you just have to iterate over the array to print every element of it:
#foreach ($item->old_values as $value)
<p>{{ $value }}</p>
#endforeach
I have simple issue with my Laravel application.But i can not find any solution of it.Please see my code here:
controller:
public function index(){
$members = Member::paginate(15);
$meal_entry = Member::all();
$breakfast = DB::SELECT("SELECT self_meal_breakfast + guest_meal_breakfast AS breakfast FROM meal_entry");
$lunch = DB::SELECT("SELECT self_meal_lunch + guest_meal_lunch AS lunch FROM meal_entry");
$dinner = DB::SELECT("SELECT self_meal_dinner + guest_meal_dinner AS dinner FROM meal_entry");
$total_meal = DB::SELECT("SELECT self_meal_breakfast + self_meal_lunch + self_meal_dinner + guest_meal_breakfast + guest_meal_lunch + guest_meal_dinner AS total FROM meal_entry");
return view('member.mealView', compact('members','data','breakfast','lunch','dinner','meal_entry','total_meal'));
}
blade view:
<tbody>
<tr>
#foreach($meal_entry as $value)
<td>{{ $value->created_at }}</td>
#endforeach
#foreach($breakfast as $value)
<td>{{ $value->breakfast }}</td>
#endforeach
#foreach($lunch as $value)
<td>{{ $value->lunch }}</td>
#endforeach
#foreach($dinner as $value)
<td>{{ $value->dinner }}</td>
#endforeach
#foreach($total_meal as $value)
<td>{{ $value->total }}</td>
#endforeach
#foreach($meal_entry as $value)
<td>{{ $value->created_at }}</td>
#endforeach
</tr>
</tbody>
And the table: meal_entry.
If there is a one row in the database then its ok ( see ) to view.But when there is row more than one then its show in one row with all data( see ).How can i handle this things? I think it should be run another loop but how can i do that? Thanks in advance.
So, if you're using Laravel, one of the best features that has is the accessors:
https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators
You can create new attributes and use them as model attributes.
For some stuff are very useful.
class Meal extends Model
{
public function getTotalBreakfastAttribute()
{
return $this->self_meal_breakfast + $this->guest_meal_breakfast;
}
}
And then, use this accessor as attribute:
<tbody>
#foreach($foods as $value)
<tr>
<td> {{$value->total_breakfast}} </td>
<td> {{$value->created_at}} </td>
</tr>
#endforeach
</tbody>
Or if you don't want to do that, instead of use multiple queries to get the data, just use one because all the fields are in the same table, and then, you could use the above example.
Enjoy! :)
A quick patch would be to do only one query and then do the calculations on the view (I know that is not a good practice to do the calculations in the view, but is the easiest way that i can see).
It would be like this:
Controller:
public function index(){
$members = Member::paginate(15);
return view('member.mealView', compact('members'));
}
blade view
<tbody>
#foreach($members as $meal_entry)
<tr>
<td>{{ $meal_entry->created_at }}</td>
<td>{{ $breakfast = $meal_entry->self_meal_breakfast + $meal_entry->guest_meal_breakfast }}</td>
<td>{{ $lunch = $meal_entry->self_meal_lunch+ $meal_entry->guest_meal_lunch}}</td>
<td>{{ $dinner = $meal_entry->self_meal_dinner + $meal_entry->guest_meal_dinner }}</td>
<td>{{ $breakfast + $lunch + $dinner }}</td>
<td>{{ $meal_entry->created_at }}</td>
</tr>
#endforeach
</tbody>
And remember to add
{{ $members->links() }}
Where you want the pagination links.
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);
}