我正在尝试使用Spatie / Activitylog activity_log表的属性属性基于Task模型的employee_id获取员工姓名。
我的模特:
use LogsActivity;
protected $fillable = ['id','employee_id', 'name', 'description'......];
protected static $logAttributes = ['id','employee_id','name', 'description'......];
protected static $logFillable = true;
protected static $logUnguarded = true;
我的控制器:
{
$activity = Activity::orderBy('id', 'DESC')->paginate(15);
return view('adminlte::home', ['activity' => $activity]);
}
我的刀片:
@foreach($activity as $act)
{{$act->changes['attributes']['employee_id']}}
@endforeach
保存在属性字段中的记录:
{"attributes":{"id":170,"employee_id":"[\"1\",\"2\"]","name":"test","description":"test",......}}
此外,在我的刀片中,结果是:
|employee_id | name | description| ...
|------------|------------|------------|--------
|["1","2"] | test | test | ....
问题是,如何基于employee_id获取名称字段。例如,在这种情况下,乔恩·大卫(不是他们的ID([“ 1”,“ 2”]))。因此,我想获取名称而不是ID。
谢谢。
答案 0 :(得分:0)
我将从数据库中获取它们
1)创建ID列表并获取它们。
$employees = Employee::whereIn('id', $activity->pluck('employee_id')->flatten())
->get()
->mapWithKeys(function ($employee) {
return [$employee->id => $employee];
});
2)将其发送到前端
return view('adminlte::home', ['activity' => $activity, 'employees' => $employees]);
3)使用映射显示名称
@foreach($activity as $act)
@foreach($act->changes['attributes']['employee_id'] as $employeeId)
{{ $employees[$employeeId] }},
@endforeach
@endforeach