Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mini-office
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
trainee
mini-office
Commits
36003fc5
Commit
36003fc5
authored
Nov 05, 2026
by
Amshar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
EXERCISE MONTH 2 CODING CHALLENGE
parent
c0855985
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
175 additions
and
32 deletions
+175
-32
app/Http/Controllers/PositionController.php
+12
-4
app/Http/Controllers/StaffController.php
+21
-4
app/Http/Requests/StoreStaffRequest.php
+1
-1
app/Http/Requests/UpdateStaffRequest.php
+3
-2
app/Models/Position.php
+12
-0
app/Models/Staff.php
+9
-2
database/factories/StaffFactory.php
+4
-2
database/migrations/2026_11_05_010954_replace_position_with_position_id_on_staff_table.php
+39
-0
database/seeders/StaffSeeder.php
+23
-5
resources/views/staff/_form.blade.php
+35
-10
resources/views/staff/index.blade.php
+15
-1
resources/views/staff/show.blade.php
+1
-1
No files found.
app/Http/Controllers/PositionController.php
View file @
36003fc5
...
@@ -86,9 +86,17 @@ public function update(Request $request, Position $position)
...
@@ -86,9 +86,17 @@ public function update(Request $request, Position $position)
* Remove the specified resource from storage.
* Remove the specified resource from storage.
*/
*/
public
function
destroy
(
Position
$position
)
public
function
destroy
(
Position
$position
)
{
{
$position
->
delete
();
// Don't delete a position that still has staff assigned to it.
// Same safety check as DepartmentController — the foreign key
return
back
()
->
with
(
'success'
,
'Position deleted.'
);
// in MySQL would block it anyway (restrictOnDelete), but this
// gives the user a friendly message instead of an error page.
if
(
$position
->
staff
()
->
exists
())
{
return
back
()
->
with
(
'error'
,
'Cannot delete this position. Move or remove its staff first.'
);
}
}
$position
->
delete
();
return
back
()
->
with
(
'success'
,
'Position deleted.'
);
}
}
}
app/Http/Controllers/StaffController.php
View file @
36003fc5
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
use
App\Http\Requests\UpdateStaffRequest
;
use
App\Http\Requests\UpdateStaffRequest
;
use
App\Models\Staff
;
use
App\Models\Staff
;
use
App\Models\Department
;
use
App\Models\Department
;
use
App\Models\Position
;
use
Illuminate\Http\Request
;
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\Storage
;
use
Illuminate\Support\Facades\Storage
;
...
@@ -42,7 +43,7 @@ public function index(Request $request)
...
@@ -42,7 +43,7 @@ public function index(Request $request)
?
$request
->
sort
?
$request
->
sort
:
'name'
;
:
'name'
;
$staff
=
Staff
::
with
(
'department'
)
$staff
=
Staff
::
with
(
[
'department'
,
'position'
]
)
->
when
(
$request
->
search
,
function
(
$query
,
$term
)
{
->
when
(
$request
->
search
,
function
(
$query
,
$term
)
{
// Wrap the OR conditions in a group so the department
// Wrap the OR conditions in a group so the department
// filter still works. Without the group, a department
// filter still works. Without the group, a department
...
@@ -58,6 +59,10 @@ public function index(Request $request)
...
@@ -58,6 +59,10 @@ public function index(Request $request)
// Filter by the department ID from the dropdown
// Filter by the department ID from the dropdown
$query
->
where
(
'department_id'
,
$departmentId
);
$query
->
where
(
'department_id'
,
$departmentId
);
})
})
->
when
(
$request
->
position
,
function
(
$query
,
$positionId
)
{
// Filter by the position ID from the dropdown
$query
->
where
(
'position_id'
,
$positionId
);
})
// Sort based on the dropdown selection
// Sort based on the dropdown selection
->
when
(
$sort
===
'newest'
,
function
(
$query
)
{
->
when
(
$sort
===
'newest'
,
function
(
$query
)
{
// Most recent join date first
// Most recent join date first
...
@@ -81,8 +86,11 @@ public function index(Request $request)
...
@@ -81,8 +86,11 @@ public function index(Request $request)
// The department list is for the filter dropdown, not the staff data
// The department list is for the filter dropdown, not the staff data
$departments
=
Department
::
orderBy
(
'name'
)
->
get
();
$departments
=
Department
::
orderBy
(
'name'
)
->
get
();
// Same thing for positions — we need the full list so the
// filter dropdown can show all available positions to pick from.
$positions
=
\App\Models\Position
::
orderBy
(
'name'
)
->
get
();
return
view
(
'staff.index'
,
compact
(
'staff'
,
'departments'
));
//The result of quuery above is stored in $staff, which then gets passed
to the view
return
view
(
'staff.index'
,
compact
(
'staff'
,
'departments'
,
'positions'
));
// Pass the staff results, department list and position list
to the view
}
}
/**
/**
...
@@ -93,9 +101,15 @@ public function index(Request $request)
...
@@ -93,9 +101,15 @@ public function index(Request $request)
*/
*/
public
function
create
()
public
function
create
()
{
{
// We need the full list of departments and positions so the
// form can show them as dropdown options for the user to pick from.
$departments
=
Department
::
orderBy
(
'name'
)
->
get
();
$departments
=
Department
::
orderBy
(
'name'
)
->
get
();
$positions
=
Position
::
orderBy
(
'name'
)
->
get
();
return
view
(
'staff.create'
,
compact
(
'departments'
));
// Without this return, the method ends with nothing to send back,
// so Laravel replies with an empty page. Always return a view
// (or a redirect) from a controller method.
return
view
(
'staff.create'
,
compact
(
'departments'
,
'positions'
));
}
}
/**
/**
...
@@ -167,9 +181,12 @@ public function show(Staff $staff)
...
@@ -167,9 +181,12 @@ public function show(Staff $staff)
*/
*/
public
function
edit
(
Staff
$staff
)
public
function
edit
(
Staff
$staff
)
{
{
// Same dropdown lists as create(), plus the existing staff record
// so the form can pre-fill every field with their current values.
$departments
=
Department
::
orderBy
(
'name'
)
->
get
();
$departments
=
Department
::
orderBy
(
'name'
)
->
get
();
$positions
=
\App\Models\Position
::
orderBy
(
'name'
)
->
get
();
return
view
(
'staff.edit'
,
compact
(
'staff'
,
'departments'
));
return
view
(
'staff.edit'
,
compact
(
'staff'
,
'departments'
,
'positions'
));
}
}
/**
/**
...
...
app/Http/Requests/StoreStaffRequest.php
View file @
36003fc5
...
@@ -32,7 +32,7 @@ public function rules(): array
...
@@ -32,7 +32,7 @@ public function rules(): array
'name'
=>
'required|max:100'
,
'name'
=>
'required|max:100'
,
'email'
=>
'required|email|unique:staff'
,
'email'
=>
'required|email|unique:staff'
,
'department_id'
=>
'required|exists:departments,id'
,
'department_id'
=>
'required|exists:departments,id'
,
'position
'
=>
'required|max:100
'
,
'position
_id'
=>
'required|exists:positions,id
'
,
'join_date'
=>
'required|date|before_or_equal:today'
,
'join_date'
=>
'required|date|before_or_equal:today'
,
'photo'
=>
'nullable|image|mimes:jpg,png,webp|max:2048'
,
// 2MB
'photo'
=>
'nullable|image|mimes:jpg,png,webp|max:2048'
,
// 2MB
'contract'
=>
'nullable|file|mimes:pdf|max:5120'
,
// 5MB
'contract'
=>
'nullable|file|mimes:pdf|max:5120'
,
// 5MB
...
...
app/Http/Requests/UpdateStaffRequest.php
View file @
36003fc5
...
@@ -55,14 +55,14 @@ public function rules(): array
...
@@ -55,14 +55,14 @@ public function rules(): array
Rule
::
unique
(
'staff'
)
->
ignore
(
$this
->
route
(
'staff'
)),
Rule
::
unique
(
'staff'
)
->
ignore
(
$this
->
route
(
'staff'
)),
],
],
'department_id'
=>
'required|exists:departments,id'
,
'department_id'
=>
'required|exists:departments,id'
,
'position
'
=>
'required|max:100
'
,
'position
_id'
=>
'required|exists:positions,id
'
,
'join_date'
=>
'required|date|before_or_equal:today'
,
'join_date'
=>
'required|date|before_or_equal:today'
,
'photo'
=>
'nullable|image|mimes:jpg,png,webp|max:2048'
,
'photo'
=>
'nullable|image|mimes:jpg,png,webp|max:2048'
,
];
];
}
}
/**
/**
* Custom error messages — same as StoreStaffRequest.
* Custom error messages — same as StoreStaffRequest.
these custom errors comes out if those errors happen
*/
*/
public
function
messages
()
:
array
public
function
messages
()
:
array
{
{
...
@@ -70,6 +70,7 @@ public function messages(): array
...
@@ -70,6 +70,7 @@ public function messages(): array
'staff_no.unique'
=>
'This staff number is already taken.'
,
'staff_no.unique'
=>
'This staff number is already taken.'
,
'email.unique'
=>
'This email address is already in use.'
,
'email.unique'
=>
'This email address is already in use.'
,
'department_id.exists'
=>
'Please select a valid department.'
,
'department_id.exists'
=>
'Please select a valid department.'
,
'position_id.exists'
=>
'Please select a valid position.'
,
'join_date.before_or_equal'
=>
'The join date cannot be in the future.'
,
'join_date.before_or_equal'
=>
'The join date cannot be in the future.'
,
];
];
}
}
...
...
app/Models/Position.php
View file @
36003fc5
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Relations\HasMany
;
class
Position
extends
Model
class
Position
extends
Model
{
{
...
@@ -16,4 +17,15 @@ class Position extends Model
...
@@ -16,4 +17,15 @@ class Position extends Model
'description'
,
'description'
,
'is_active'
,
'is_active'
,
];
];
/**
* A Position has many staff members.
* This is the other side of the relationship — from the "one" looking
* down at the "many". Laravel knows to look for position_id on the
* staff table because this model is called Position.
*/
public
function
staff
()
:
HasMany
{
return
$this
->
hasMany
(
Staff
::
class
);
}
}
}
app/Models/Staff.php
View file @
36003fc5
...
@@ -23,7 +23,7 @@ class Staff extends Model
...
@@ -23,7 +23,7 @@ class Staff extends Model
'email'
,
'email'
,
'photo_path'
,
'photo_path'
,
'contract_path'
,
'contract_path'
,
'position
'
,
'position
_id'
,
// changed from position to position_id for the exercise momth 2
'join_date'
,
'join_date'
,
];
];
...
@@ -42,11 +42,18 @@ class Staff extends Model
...
@@ -42,11 +42,18 @@ class Staff extends Model
* Laravel looks for a department_id column on this table
* Laravel looks for a department_id column on this table
* and matches it to the id column on the departments table.
* and matches it to the id column on the departments table.
*/
*/
public
function
department
()
:
BelongsTo
public
function
department
()
:
BelongsTo
// This one department can have many staff. We wrote this at the many side
{
{
return
$this
->
belongsTo
(
Department
::
class
);
return
$this
->
belongsTo
(
Department
::
class
);
}
}
// This is the "many" side: each staff member belongs to one position,
// just like each staff member belongs to one department.
public
function
position
()
:
BelongsTo
// This one position can have many staff. We wrote this at the many side
{
return
$this
->
belongsTo
(
Position
::
class
);
}
/**
/**
* Accessor: $staff->photo_url
* Accessor: $staff->photo_url
*
*
...
...
database/factories/StaffFactory.php
View file @
36003fc5
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
use
App\Models\Department
;
use
App\Models\Department
;
use
App\Models\Staff
;
use
App\Models\Staff
;
use
App\Models\Position
;
use
Illuminate\Database\Eloquent\Factories\Factory
;
use
Illuminate\Database\Eloquent\Factories\Factory
;
/**
/**
...
@@ -33,8 +34,9 @@ public function definition(): array
...
@@ -33,8 +34,9 @@ public function definition(): array
// A safe email that won't accidentally send to a real address (uses @example.com)
// A safe email that won't accidentally send to a real address (uses @example.com)
'email'
=>
fake
()
->
unique
()
->
safeEmail
(),
'email'
=>
fake
()
->
unique
()
->
safeEmail
(),
// A random job title like "Accountant" or "Software Engineer"
// Pick a random position from the positions table,
'position'
=>
fake
()
->
jobTitle
(),
// same pattern as department_id in the seeder.
'position_id'
=>
\App\Models\Position
::
factory
(),
// A random date within the last 5 years, so join dates look realistic
// A random date within the last 5 years, so join dates look realistic
'join_date'
=>
fake
()
->
dateTimeBetween
(
'-5 years'
),
'join_date'
=>
fake
()
->
dateTimeBetween
(
'-5 years'
),
...
...
database/migrations/2026_11_05_010954_replace_position_with_position_id_on_staff_table.php
0 → 100644
View file @
36003fc5
<?php
use
Illuminate\Database\Migrations\Migration
;
use
Illuminate\Database\Schema\Blueprint
;
use
Illuminate\Support\Facades\Schema
;
return
new
class
extends
Migration
{
/**
* Replace the plain-text "position" column with a foreign key
* pointing to the positions table. Same pattern as department_id.
*/
public
function
up
()
:
void
{
Schema
::
table
(
'staff'
,
function
(
Blueprint
$table
)
{
// Remove the old free-text column
$table
->
dropColumn
(
'position'
);
// Add a foreign key to the positions table.
// restrictOnDelete() stops deleting a position that has staff.
$table
->
foreignId
(
'position_id'
)
->
after
(
'email'
)
->
constrained
()
->
restrictOnDelete
();
});
}
/**
* Reverse: drop the foreign key, bring back the text column.
*/
public
function
down
()
:
void
{
Schema
::
table
(
'staff'
,
function
(
Blueprint
$table
)
{
$table
->
dropConstrainedForeignId
(
'position_id'
);
$table
->
string
(
'position'
,
100
)
->
after
(
'email'
);
});
}
};
\ No newline at end of file
database/seeders/StaffSeeder.php
View file @
36003fc5
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
Database\Seeders
;
namespace
Database\Seeders
;
use
App\Models\Department
;
use
App\Models\Department
;
use
App\Models\Position
;
use
App\Models\Staff
;
use
App\Models\Staff
;
use
Illuminate\Database\Console\Seeds\WithoutModelEvents
;
use
Illuminate\Database\Console\Seeds\WithoutModelEvents
;
use
Illuminate\Database\Seeder
;
use
Illuminate\Database\Seeder
;
...
@@ -11,16 +12,32 @@ class StaffSeeder extends Seeder
...
@@ -11,16 +12,32 @@ class StaffSeeder extends Seeder
{
{
/**
/**
* Seed 10 staff members into each existing department.
* Seed 10 staff members into each existing department.
* We loop through every department and use the factory to create 10 staff,
*
* but we override department_id so they join the real department
* We loop through every department and use the factory to create
* instead of the factory creating a random new one.
* 10 staff, but we override department_id and position_id so
* they join real departments and positions instead of the factory
* creating random new ones.
*/
*/
public
function
run
()
:
void
public
function
run
()
:
void
{
{
Department
::
all
()
->
each
(
function
(
$department
)
{
// Grab all position IDs upfront so we can pick one randomly
// for each staff member. pluck('id') returns a collection
// like [1, 2, 3, 4, 5] — just the IDs, not the full records.
// We do this OUTSIDE the loop so it only runs one query,
// not one per department.
$positionIds
=
Position
::
pluck
(
'id'
);
Department
::
all
()
->
each
(
function
(
$department
)
use
(
$positionIds
)
{
Staff
::
factory
(
10
)
->
create
([
Staff
::
factory
(
10
)
->
create
([
// Override so staff join THIS department, not a random factory one
'department_id'
=>
$department
->
id
,
'department_id'
=>
$department
->
id
,
// fn () => makes this run FRESH for each of the 10 staff.
// Without fn (), random() would run once and all 10 staff
// would get the same position. The arrow function says
// "pick a new random position for each person."
'position_id'
=>
fn
()
=>
$positionIds
->
random
(),
]);
]);
});
});
}
}
}
}
\ No newline at end of file
resources/views/staff/_form.blade.php
View file @
36003fc5
...
@@ -150,18 +150,43 @@
...
@@ -150,18 +150,43 @@
</div>
</div>
<div class="
row
mb
-
3
">
<div class="
row
mb
-
3
">
{{-- Position --}}
{{--
Position Dropdown
Same idea as the department dropdown above. Instead of
letting the user type a position name as free text, we
show a <select> with only valid positions from the
positions table. The value sent to the server is the
position's ID, not its name.
The Form Request rule 'exists:positions,id' double-checks
that the ID actually exists, in case someone tampers with
the form using DevTools.
--}}
<div class="
col
-
md
-
6
">
<div class="
col
-
md
-
6
">
<label for="
position
" class="
form
-
label
">Position</label>
<label for="
position_id
" class="
form
-
label
">Position</label>
<input
<select
type="
text
"
name="
position_id
"
name="
position
"
id="
position_id
"
id="
position
"
class="
form
-
select
@
error
(
'position_id'
)
is
-
invalid
@
enderror
"
class="
form
-
control
@
error
(
'position'
)
is
-
invalid
@
enderror
"
value="
{{
old
(
'position'
,
$staff
->
position
??
''
)
}}
"
maxlength="
100
"
>
>
@error('position')
<option value="">-- Select Position --</option>
@foreach (
$positions
as
$position
)
{{--
Same pattern as department: old() checks if the
form just failed, then falls back to the saved
value on edit, then empty on create.
--}}
<option
value="
{{
$position
->
id
}}
"
@selected(old('position_id',
$staff->position_id
?? '') ==
$position->id
)
>
{{
$position->name
}}
</option>
@endforeach
</select>
@error('position_id')
<div class="
invalid
-
feedback
">{{
$message
}}</div>
<div class="
invalid
-
feedback
">{{
$message
}}</div>
@enderror
@enderror
</div>
</div>
...
...
resources/views/staff/index.blade.php
View file @
36003fc5
...
@@ -56,6 +56,20 @@
...
@@ -56,6 +56,20 @@
</select>
</select>
</div>
</div>
{{-- Position filter — same pattern as the department filter.
Sends ?position=5 in the URL so the controller can filter by it. --}}
<div class="
col
-
md
-
3
">
<select name="
position
" class="
form
-
select
">
<option value="">All positions</option>
@foreach (
$positions
as
$position
)
<option value="
{{
$position
->
id
}}
"
@selected(request('position') ==
$position->id
)>
{{
$position->name
}}
</option>
@endforeach
</select>
</div>
{{--
{{--
Sort dropdown.
Sort dropdown.
The three options match exactly what the controller accepts.
The three options match exactly what the controller accepts.
...
@@ -137,7 +151,7 @@
...
@@ -137,7 +151,7 @@
Without
eager
loading
,
this
would
run
one
query
per
row
.
Without
eager
loading
,
this
would
run
one
query
per
row
.
--
}}
--
}}
<
td
>
{{
$s
->
department
->
name
}}
</
td
>
<
td
>
{{
$s
->
department
->
name
}}
</
td
>
<
td
>
{{
$s
->
position
}}
</
td
>
<
td
>
{{
$s
->
position
->
name
}}
</
td
>
{{
--
Show
the
position
name
from
the
relationship
,
not
a
text
field
--
}}
<
td
>
{{
$s
->
join_date
->
format
(
'd M Y'
)
}}
</
td
>
<
td
>
{{
$s
->
join_date
->
format
(
'd M Y'
)
}}
</
td
>
<
td
>
<
td
>
<
a
href
=
"{{ route('staff.edit',
$s
) }}"
class
="
btn
btn
-
sm
btn
-
outline
-
primary
">Edit</a>
<
a
href
=
"{{ route('staff.edit',
$s
) }}"
class
="
btn
btn
-
sm
btn
-
outline
-
primary
">Edit</a>
...
...
resources/views/staff/show.blade.php
View file @
36003fc5
...
@@ -103,7 +103,7 @@
...
@@ -103,7 +103,7 @@
</tr>
</tr>
<tr>
<tr>
<th>Position</th>
<th>Position</th>
<td>{{ $staff->position
}}</td>
<td>{{ $staff->position
->name }}</td> {{-- Was: $staff->position --}}
</tr>
</tr>
<tr>
<tr>
{{--
{{--
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment