Таблица базы данных
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
Предполагается, что у вас есть пользовательская таблица с полем "username"
Всякий раз, когда пользователь обновляет отведение, вам просто нужно вставить запись в отведениитаблица истории с этим идентификатором пользователя в поле updated_by