Updated June 2026. Tested on Laravel 13 and PHP 8.4.
A one to many relationship, also called a has many, is when one row in a parent table matches many rows in a child table. This is the relationship you use most often.
A clear example is an employee and their attendance records. One employee clocks in and out many times, so one Employee has many Attendance rows. We will wire that up here.
The models
We will assume the tables and models already exist. If you need a refresher on writing the migration and model, see the earlier articles on migrations and models, then come back.
The Employee model holds the basic staff details.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Employee extends Model
{
use SoftDeletes;
protected $fillable = ['name', 'email', 'telephone', 'gender', 'join_date'];
}
The Attendance model holds one clock in or clock out event, and carries an employee_id foreign key that points back to the employee it belongs to.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Attendance extends Model
{
protected $fillable = [
'employee_id', 'direction', 'remarks',
'attendance_date', 'attendance_time', 'location',
];
protected $casts = [
'attendance_date' => 'date',
];
}
Define the relationship
On the Employee model, add a method that returns hasMany. The plural name reads well because it returns a collection.
use Illuminate\Database\Eloquent\Relations\HasMany;
public function attendances(): HasMany
{
return $this->hasMany(Attendance::class);
}
Then define the inverse on the Attendance model with belongsTo, so you can get from an attendance row back to its employee.
use Illuminate\Database\Eloquent\Relations\BelongsTo;
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class);
}
Because the foreign key follows the employee_id convention, Eloquent finds it without any extra arguments.
Save related records
Create the attendance through the employee. Saving this way sets the employee_id for you.
$john = Employee::find(245);
$john->attendances()->create([
'direction' => 'in',
'remarks' => 'Start',
'attendance_date' => '2026-06-15',
'attendance_time' => '08:00',
'location' => 'Office',
]);
$john->attendances()->create([
'direction' => 'out',
'remarks' => 'End',
'attendance_date' => '2026-06-15',
'attendance_time' => '17:00',
'location' => 'Office',
]);
Loop the records in a view
The related records come back as a normal collection, so you loop them like any other. Pass the employee to the view from the controller.
public function show(Employee $employee)
{
return view('employees.show', compact('employee'));
}
Then in resources/views/employees/show.blade.php, walk the attendances.
<h1>{{ $employee->name }}</h1>
<h2>Attendance</h2>
@forelse ($employee->attendances as $attendance)
<li>{{ $attendance->attendance_date->format('d M Y') }} at {{ $attendance->attendance_time }}</li>
@empty
<p>There are no attendance records yet.</p>
@endforelse
@forelse is handy here because it gives you a clean empty case without a separate if check.
Filter related records
Often you want only some of the related rows. Call the relationship as a method, which gives you a query you can add conditions to, then run it.
$todays = Employee::find(245)
->attendances()
->where('attendance_date', '2026-06-10')
->get();
That is a one to many relationship end to end: a foreign key on the child, hasMany on the parent, belongsTo on the child, and the related rows behaving like any collection you can loop and filter. Questions about one to many in Laravel are welcome in the comments.
All comments ()
No comments yet
Be the first to leave a comment on this post.