Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomek's avatar

Need some help with Laravel project & SQL / Eloquent

I must say i am really new in Laravel and Eloquent and I am able to express what data I like to have in sort of SQL but I would like to use the ELoquent approach as much as possible.

In general I need some help with my laravel project, it's my first and I dont know if I utelize it properly.

If you have some time to skype me and look through my stuff with me, that would be appreciated.

The database has data (except the Log table) .. my Laravel GUI is not yet developed to submit tasks into the LOG table but that can be easily performed through tinker..

The Project: Cleaning Tasklist Tables: Location, Area, Tasks, Task Schedule, Cleaning Log

a LOCATION can have one or more AREAs for example

House->Bathroom
House->Kitchen
Garden->Swimmingpool

and of course each AREA has a variety of TASK specific to the AREA like

Garden->Swimmingpool->Change Filters
Garden->Swimmingpool->Add Chlorine
House->Kitchen->Empty Bins
House->General->Put Bins Outside

These TASKs do not have to be done each day, hence we have a TASK SCHEDULE table where everything is defined for example: what day a task has to be started what day a task has to be finished by how many times the task has to be performed

Garden->Swimmingpool->Clean Filters
    Start: Tuesday
    Finish: Friday
    Times: 1x
    
House->General->Take a Shower
    Start: Monday
    Finish: Monday
    Times: 2x

House->General->Take a Shower
    Start: Wednesday
    Finish: Wednesday
    Times: 3x
    

Basically depending on the Weekday I want to receive all the TASKs which have NOT been logged in the CLEANING LOG table.

For example today is Wednesday, and the cleaning log has for today's date 3x entries of the House->General->Take a Shower already, then I would not get the task returned since it has been completed, if I can just find 2 entries in the log table then obviously the task has not been completed yet (1x entry is still missing).

On my index page I would then display sorted by Location, Area all the outstanding tasks for the Weekday.

I am not sure how to tackle this thing, I tried now for couple of hours to formulate this with Eloquent and my models but unsuccessful.

However I can express it in RAW SQL - if I have to...:

SELECT DISTINCT      
        AreaCleaningTaskPerDay.id AS cleaningtaskperdayid,
        AreaCleaningTask.task, 
        dbo.Location.id, 
        AreaCleaningTaskPerDay.amount
    FROM            
        dbo.AreaCleaningTask AS AreaCleaningTask INNER JOIN
        dbo.AreaCleaningTaskPerDay AS AreaCleaningTaskPerDay 
                        ON AreaCleaningTask.id = AreaCleaningTaskPerDay.fk_areacleaningtask_id 
                        INNER JOIN
        dbo.Area 
                        ON AreaCleaningTask.fk_area_id = dbo.Area.id 
                        INNER JOIN
        dbo.Location 
                        ON dbo.Area.fk_location_id = dbo.Location.id 
                        LEFT OUTER JOIN
        dbo.CleaningLog AS CleaningLog 
                        ON AreaCleaningTaskPerDay.id = CleaningLog.fk_areacleaningtaskperday_id
    WHERE        
                (AreaCleaningTaskPerDay.inactive = 0) 
            AND (AreaCleaningTaskPerDay.fk_dayofweek_id <= dbo.GetDayOfWeek()) 
            AND (AreaCleaningTaskPerDay.fk_dayofweek_id + AreaCleaningTaskPerDay.daystofinish >= dbo.GetDayOfWeek())
            AND (dbo.Location.id = ISNULL(null,dbo.Location.id)) -- if the locationid is null then basically this condition will be ignored
            AND (dbo.Area.id = ISNULL(null,dbo.Area.id))                -- if the areaId is null then basically this condition will be ignored
    GROUP BY 
        AreaCleaningTaskPerDay.id, 
        AreaCleaningTask.task, 
        dbo.Location.id, 
        AreaCleaningTaskPerDay.amount
    HAVING         
        (AreaCleaningTaskPerDay.amount - COUNT(CleaningLog.fk_areacleaningtaskperday_id) > 0)

something along this lines gives me the correct TASK id's back which I then can use to access the AREA and LOCATION off these...

please please assist me with my first project, i really dont know what the best approach would be - stored procedure with this massive SQL statements, raw SQL in laravel files, eloquent... i dont event know if my setup is correct :-(

0 likes
1 reply
Devmaurice's avatar
   I must say i am really new in Laravel.

For someone who is new you are biting a big chunk for a small mouth. Be specific on a small problem that way you will get a good answer. I don't see anyone helping you on a whole project. " My opinion"

Please or to participate in this conversation.