- copy env.example to .env
- run
docker-compose up -d --build
- after the build run
docker-compose exec php chown -R laravel:laravel /var/www/html/storage /var/www/html/bootstrap/cache
to give laravel user group access to these folders
I already have laravel installer on my Windows machine.
- I ran the command
laravel new test_rowshr
- I'll be using Wamp server with a vhost for now for faster development.
- I will be using the latest version of Laravel 11.x (11.9 as in composer.json) with PHP 8.3.6
- Enable the required php extensions from the documentation
- Create a mysql database. It is good practice (if not imperative) to also create a user for the database, but in my case I will leave it as root since it is just a test and will most likely not be deployed anywhere
-
I start by running the following commands to generate the 3 models and migrations using laravel stubs
-
php artisan make:model -m Employee
-
php artisan make:model -m Project
-
php artisan make:model -m EmployeeProject
-
I deleted the default laravel migrations and the User model, since we won't be dealing with those
-
I filled in the base migrations logic for employees, projects and empoyee_projects
-
php artisan migrate
-
Now I get an error that laravel cannot find the
sessions
table. This is because the driver for sessions is set to database, so we can adjust that in theconfig.php
or.env
. Let's do it in .env, change theSESSION_DRIVER
var to basicfile
-
I added the model relations for Project and Employee,
withPivot('role')
because, if not specified only the keys will be selected.
I should make a repository for this test at this point. I will use the Github Desktop app for simplicity
- I think now it's a good time to make some seeds so we can test if the relations are working as intended. This will jump to the task 2.1, but I'll come back to the other functions in a moment
php artisan make:seed EmployeeSeeder
andphp artisan make:factory EmployeeFactory
php artisan make:seed ProjectSeeder
andphp artisan make:factory ProjectFactory
- the factories are pretty basic, only
name
andtitle
- Since Employees can have different roles on the Projects and not themselves, I will attach them from the Project Seeder. I will use the function
each()
to do that. I will also add the timestamps to make the db 'heavier' and have uniformity with irl examples - Now I can test the relations with tinker, firing
App\Models\Employee::first()->projects()->get()
andApp\Models\Project::first()->employees()->get()
returns data and works fine 😂 - Now for the extra functions in task 1.4 and task 1.5
- Since this is a test I will write an additional function for the first point of task 1.4, but I could just call the Eloquest relation
employees
andget()
method or call$project->employees
as attribute. Let's just map thepivot->role
attribute to the employee itself asrole
- For counting I will have to use
DB::raw
since role is not a Model to usewithCount
, also invoke the relation, not the collection and return as$role => $count
- For Employee's projects function I will copy paste the
getEmployees
funciton from Project since from a functional perspective is the same and adjust the semantics
- actually task 2.2 since 2.1 is already implemented for testing purposes in Task 1.
- create 2 controllers
php artisan make:controller ProjectController
andphp artisan make:controller EmployeeController
- for Project listing task 2.2 I will simply use the
with
function that already specifieswithPivot('role')
- for the filtering of task 2.3 I will use
whereHas
and a $role parameter - add the Routes to
web.php
- Before moving to task 3 I would like to start with the bonus task with Vue.Js since for task 3 I would have to use
web.php
instead ofapi.php
. Also I want to have an interface for these functions myself.
- I will not separate the frontend and backend from the root directory.
- I will use Laravel's default module builder, vite and install
@vitejs/plugin-vue
php artisan install:api
and delete thepersonal_access_token
migration since there is no authentication- Now that vue is working I will install
vue-router
and setup the standardApp.vue
withrouter.js
- I will create a folter for employees components and one for project components
- There isn't much to talk about the vue part, it's just coding. Oh and I used tailwind since it comes preinstalled and it's my fav css framework. You should see the frontend for yourself
- I tried to include all the functions, but in such a way that I can use relations and not flood the query requests for the db. For example the count of roles is done after clicking a project
-
- likely the most complex function so far for the backend. Most Notably I used the type paramenter with automatically converts from the id to the model object, and the role as a request parameter since we are using it as string
-
- basic
delete request
- basic
-
- using
detach
method from laravel
- using
- read Optimisation.md