数据库表
CREATE TABLE `leads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `lead_history` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lead_id` int(11) unsigned DEFAULT NULL,
`updated_by` int(11) unsigned DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
型号
class Lead extends Model {
public function history()
{
return $this->hasMany(LeadHistory::class,'lead_id');
}
}
class LeadHistory extends Model{
public function lead()
{
return $this->belongsTo(Lead::class);
}
public function user()
{
return $this->belongsTo(User::class, 'updated_by');
}
}
现在获取线索
$leads = Lead::all(); //for demo purpose, always use pagination based results or fetch with condition
这里是模板渲染
@foreach ($leads as $lead)
<p>This is your lead: {{ $lead->name }}</p>
@if ($lead->history)
<label>Lead Updated By:</label>
<ul>
@foreach ($lead->history as $history)
<li>{{$history->user->username}}</li>
@endforeach
</ul>
@endif
@endforeach
假设您有带有“用户名”字段的用户表
每当用户更新潜在客户时,您只需在潜在客户历史记录表中插入一条记录,在 updated_by 字段中使用该 userId